ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringMvc(三)- CRUD

2022-09-08 13:02:32  阅读:243  来源: 互联网

标签:form 映射 1.3 SpringMvc CRUD 表单 添加 页面


1、springMvc的form表单

1.1 标签

<!-- springMvc的form表单标签 -->
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

1.2 form: 标签

使用springMvc的form表单,快速开发表单及数据自动回显;

原理:在数据模型中添加一个 参数名为 command 的参数,springMvc的form表单会自动映射

//跳转到添加页面
map.put("command",new Detail()); //参数没有内容

//跳转到修改页面
map.put("command",detailService.getDetail(id)); //有对应的参数内容

1.3 修改和添加页面复用

1.3.1 动态改变请求方式

<%-- 修改和添加是共用页面,页面要判断是添加功能还是修改,根据模型数据详情实体,有没有id值,有修改,无增加   --%>
<c:if test="${!empty command.id}" var="flag">
    <form:hidden path="id"></form:hidden>
    <!-- rest 风格修改,将post请求,改为put请求 -->
    <input type="hidden" name="_method" value="put">
</c:if>

1.3.2 动态切换修改和添加标题

<c:if test="${flag}">
    <th colspan="2">修改新闻详情</th>
</c:if>
<c:if test="${!flag}">
    <th colspan="2">添加新闻详情</th>
</c:if>

1.3.3 下拉表单自动映射

<!-- 下拉列表,path属性指定的是select标签的id和name属性值(还可以根据此值从实体中获取参数,回显数据),items属性指定的集合数据,自动遍历,并添加option选项,itemLabel对应option选项的显示内容, itemValue对应option选项的value属性值 -->
<td><form:select path="cid" items="${categories}" itemLabel="name" itemValue="id"/></td>

1.3.4 普通参数自动映射

 <!-- 单行文本,path属性指定的是input标签的id和name属性值,还必须和返回的实体属性中的属性名一致,才可以回显数据 -->
<td><form:input path="title"/></td>

1.3.5 全部jsp

修改页面 和 增加页面 复用;

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- 核心标签库 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<html>
<head>
    <title>添加新闻</title>
</head>
<body>
<%-- 使用springMvc的form表单,快速开发表单及数据自动回显 --%>
<form:form action="${pageContext.request.contextPath}/detail" method="post">
    <%-- 修改和添加是共用页面,页面要判断是添加功能还是修改,根据模型数据详情实体,有没有id值,有修改,无增加   --%>
    <c:if test="${!empty command.id}" var="flag">
        <form:hidden path="id"></form:hidden>
        <!-- rest 风格修改,将post请求,改为put请求 -->
        <input type="hidden" name="_method" value="put">
    </c:if>
    <table border="1px" width="40%" cellspacing="0" align="center" style="margin-top: 96px;">
        <tr style="height: 80px; font-size: 26px; background-color: cyan;">
            <c:if test="${flag}">
                <th colspan="2">修改新闻详情</th>
            </c:if>
            <c:if test="${!flag}">
                <th colspan="2">添加新闻详情</th>
            </c:if>
        </tr>
        <tr style="height: 50px; font-size: 18px;">
            <td style="background-color: cyan;">新闻分类:</td>
            <!-- 下拉列表,path属性指定的是select标签的id和name属性值(还可以根据此值从实体中获取参数,回显数据),items属性指定的集合数据,自动遍历,并添加option选项,itemLabel对应option选项的显示内容, itemValue对应option选项的value属性值 -->
            <td><form:select path="cid" items="${categories}" itemLabel="name" itemValue="id"/></td>
        </tr>
        <tr style="height: 50px; font-size: 18px;">
            <td style="background-color: cyan;">新闻标题:</td>
            <!-- 单行文本,path属性指定的是input标签的id和name属性值,还必须和返回的实体属性中的属性名一致,才可以回显数据 -->
            <td><form:input path="title"/></td>
        </tr>
        <tr style="height: 50px; font-size: 18px;">
            <td style="background-color: cyan;">新闻摘要:</td>
            <td><form:textarea path="summary"/></td>
        </tr>
        <tr style="height: 50px; font-size: 18px;">
            <td style="background-color: cyan;">新闻作者:</td>
            <td><form:input path="author"/></td>
        </tr>
        <tr style="height: 50px; font-size: 18px;">
            <td style="background-color: cyan;">创建时间:</td>
            <td><form:input path="createDate"/></td>
        </tr>

        <tr style="height: 50px; font-size: 18px; background-color: cyan;">
            <td colspan="2" style="text-align: center">
                <input type="submit" value="提&nbsp;&nbsp;交"/>
                <input type="button" value="返&nbsp;&nbsp;回"/>
            </td>
        </tr>
    </table>
</form:form>

</body>
</html>

1.3.6 测试

1.3.6.1 增加

1.3.6.2 修改

1.4 静态资源无法加载问题

1.4.1 静态资源无法加载

1.4.2 配置静态资源

必须同时配置 MVC 的注解扫描静态资源映射

原理:
<mvc:default-servlet-handler>这个配置,可以将在 SpringMVC 上下文中定义一个 DefaultServletHttpRequestHandler,它会对进入 DispatcherServlet 的请求进行筛查
如果发现是没有经过配置映射的请求,就将该请求交由 WEB 应用服务器默认的 Servlet 处理,在tomcat的web.xml中,配置一个叫default的servlet,对应的url-patten也是配置的 /;
记住:Springmvc的DispatcherServlet的优先级高于tomcat默认的default,所以配置映射的会访问,但是没有映射的交由tomcat处理,就可以进行访问到静态资源了,如果是有配置映射的请求,才由 DispatcherServlet 继续处理;

<!-- 开启MVC 的注解扫描 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 静态资源过滤 -->
<mvc:default-servlet-handler></mvc:default-servlet-handler>

标签:form,映射,1.3,SpringMvc,CRUD,表单,添加,页面
来源: https://www.cnblogs.com/xiaoqigui/p/16669047.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有