学校网站源码html,电子商务网站的后台管理系统,中国最大的软件外包公司,中华网军事网目录
一、创建项目#xff0c;pom文件
二、web.xml
三、spring-mvc.xml
四、index.jsp
五、实体类
Address类
User类
六、UserController类
七、请求参数解决中文乱码
八、配置tomcat,然后启动tomcat
1.
2.
3.
4.
九、接收Map类型
1.直接接收Map类型
#x…目录
一、创建项目pom文件
二、web.xml
三、spring-mvc.xml
四、index.jsp
五、实体类
Address类
User类
六、UserController类
七、请求参数解决中文乱码
八、配置tomcat,然后启动tomcat
1.
2.
3.
4.
九、接收Map类型
1.直接接收Map类型
1Get请求
第一种情况什么注解也没有
第二种情况传个值
第三种情况声明是get请求
第四种情况:加RequestParam
(2)post请求
第一种情况什么注解也没有
前端页面加一个表单
第二种情况声明是post请求
第三种情况加上RequestParam注解
表单和controller类中的方法改改(加个username)
第四种情况加RequestBody注解
2.用对象接收map
(1)User类里加一个map
(2)前端
(3运行 十、在控制器中使用原生的ServletAPI对象 一、创建项目pom文件
?xml version1.0 encodingUTF-8?project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.qcby/groupIdartifactIdspringMVC12/artifactIdversion1.0-SNAPSHOT/versionpackagingwar/packagingpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncodingspring.version5.0.2.RELEASE/spring.version/propertiesdependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-web/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion${spring.version}/version/dependencydependencygroupIdjavax.servlet/groupIdartifactIdservlet-api/artifactIdversion2.5/versionscopeprovided/scope/dependencydependencygroupIdjavax.servlet.jsp/groupIdartifactIdjsp-api/artifactIdversion2.0/versionscopeprovided/scope/dependency/dependencies/project
二、web.xml
?xml version1.0 encodingUTF-8?web-app xmlnshttp://xmlns.jcp.org/xml/ns/javaeexmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsdversion4.0!--前端控制器--servletservlet-namedispatcherServlet/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classinit-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:spring-mvc.xml/param-value/init-param!--配置启动加载--load-on-startup1/load-on-startup/servletservlet-mappingservlet-namedispatcherServlet/servlet-nameurl-pattern*.do/url-pattern/servlet-mapping/web-app
三、spring-mvc.xml
?xml version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beansxmlns:mvchttp://www.springframework.org/schema/mvcxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 配置spring创建容器时要扫描的包 --context:component-scan base-packagecom.qcby.controller/context:component-scan!-- 配置视图解析器 --bean idviewResolver classorg.springframework.web.servlet.view.InternalResourceViewResolverproperty nameprefix value/WEB-INF/pages//propertyproperty namesuffix value.jsp/property/bean!-- 配置spring开启注解mvc的支持--!-- mvc:annotation-driven/mvc:annotation-driven--/beans
四、index.jsp
% page contentTypetext/html;charsetUTF-8 languagejava %htmlheadtitle请求参数绑定/title/headbodyform actionuser/save1.do methodpost姓名input typetext nameusername /br/年龄input typetext nameage /br/input typesubmit value提交 //formh3请求参数绑定封装到实体类/h3form actionuser/save2.do methodpost姓名input typetext nameusername /br/年龄input typetext nameage /br/input typesubmit value提交 //formh3请求参数绑定封装到实体类/h3form actionuser/save3.do methodpost姓名input typetext nameusername /br/年龄input typetext nameage /br/金额input typetext nameaddress.money /br/input typesubmit value提交 //formh3请求参数绑定封装到实体类存在list集合/h3form actionuser/save4.do methodpost姓名input typetext nameusername /br/年龄input typetext nameage /br/金额input typetext nameaddress.money /br/集合input typetext namelist[0].money /br/集合input typetext namelist[1].money /br/input typesubmit value提交 //form/body/html
五、实体类
Address类
import java.io.Serializable;public class Address implements Serializable {private Double money;public Double getMoney() {return money;}public void setMoney(Double money) {this.money money;}Overridepublic String toString() {return Address{ money money };}}
User类
import java.io.Serializable;import java.util.List;public class User implements Serializable {private String username;private Integer age;// 引用对象private Address address;// list集合private ListAddress list;public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address address;}public ListAddress getList() {return list;}public void setList(ListAddress list) {this.list list;}Overridepublic String toString() {return User{ username username \ , age age , address address , list list };}}
六、UserController类
import com.qcby.pojo.User;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;ControllerRequestMapping(/user)public class UserController {RequestMapping(/save1.do)public String save(String username,Integer age){System.out.println(姓名:username);System.out.println(年龄:age);return success;}RequestMapping(/save2.do)public String save2(User user){System.out.println(user对象:user);return success;}RequestMapping(/save3.do)public String save3(User user){System.out.println(user对象:user);return success;}RequestMapping(/save4.do)public String save4(User user){System.out.println(user对象:user);return success;}}
七、请求参数解决中文乱码
在web.xml中配置Spring提供的过滤器
!-- 配置过滤器解决中文乱码的问题 --filterfilter-namecharacterEncodingFilter/filter-namefilter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-class!-- 指定字符集 --init-paramparam-nameencoding/param-nameparam-valueUTF-8/param-value/init-param/filterfilter-mappingfilter-namecharacterEncodingFilter/filter-nameurl-pattern/*/url-pattern/filter-mapping现在的web.xml?xml version1.0 encodingUTF-8?web-app xmlnshttp://xmlns.jcp.org/xml/ns/javaeexmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsdversion4.0!-- 配置过滤器解决中文乱码的问题 --filterfilter-namecharacterEncodingFilter/filter-namefilter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-class!-- 指定字符集 --init-paramparam-nameencoding/param-nameparam-valueUTF-8/param-value/init-param/filterfilter-mappingfilter-namecharacterEncodingFilter/filter-nameurl-pattern/*/url-pattern/filter-mapping!--前端控制器--servletservlet-namedispatcherServlet/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classinit-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:spring-mvc.xml/param-value/init-param!--配置启动加载--load-on-startup1/load-on-startup/servletservlet-mappingservlet-namedispatcherServlet/servlet-nameurl-pattern*.do/url-pattern/servlet-mapping/web-app
八、配置tomcat,然后启动tomcat
1. 2. 3. 4. 九、接收Map类型
1.直接接收Map类型
如果想直接接收前端传过来的map参数应该使用两个注解(RequestBody或RequestParamRequestParam--get和post请求都可以RequestBody只能post请求底层封装都是LinkedHashMap)
1Get请求
第一种情况什么注解也没有
UserController类里加一个方法
RequestMapping(/mapSave1.do)public String mapSave1(MapString, Objects map){System.out.println(map:map);return success;}
没有JSP页面启动tomcat 控制台什么输出也没有没有值 第二种情况传个值 控制台还是什么都没有 第三种情况声明是get请求
UserController类的mapSave1()方法
RequestMapping(value /mapSave1.do,method RequestMethod.GET)public String mapSave1(MapString, Objects map){System.out.println(map:map);return success;}
再启动 控制台还是没有值 所以跟请求是什么没关系要想接收就要加注解
第四种情况:加RequestParam
RequestMapping(value /mapSave1.do)public String mapSave1(RequestParam MapString, Objects map){System.out.println(map:map);return success;}
再运行 所以我传递一个map在后端接收用get请求必须加RequestParam注解
(2)post请求
第一种情况什么注解也没有
RequestMapping(value /mapSave2.do)public String mapSave1(MapString, Objects map){System.out.println(map:map);return success;}
前端页面加一个表单
h3请求参数的绑定--map集合/h3form actionuser/mapSave2.do methodpostmap集合key:input typetext namemap.key /br/map集合value:input typetext namemap.value /br/input typesubmit value提交 //form
运行 点提交 控制台什么也没有 第二种情况声明是post请求
RequestMapping(value /mapSave2.do,method RequestMethod.POST)public String mapSave2(MapString, Objects map){System.out.println(map:map);return success;}
再运行 点提交 控制台 说明跟get的一样不加注解是没有办法接收到的
第三种情况加上RequestParam注解
RequestMapping(value /mapSave2.do,method RequestMethod.POST)public String mapSave2(RequestParam MapString, Objects map){System.out.println(map:map);return success;}
运行 点提交 控制台 可以看出get请求和post请求都可以用RequestParam注解
表单和controller类中的方法改改(加个username)
表单
h3请求参数的绑定--map集合/h3form actionuser/mapSave2.do methodpostusername:input typetext nameusernamebr/map集合:input typetext nametest1br/%-- test1就是map的key输入框中的就是map的value --%input typesubmit value提交 //form
方法
RequestMapping(value /mapSave2.do)public String mapSave2(RequestParam MapString, Objects map,String username){System.out.println(map:map);System.out.println(username:username);return success;}
运行 点提交 控制台 可以看到表单中的数据都被封装到了map集合中
第四种情况加RequestBody注解
但是这样的话它只能接收json数据
现在用表单接收就会报错
RequestMapping(value /mapSave2.do)public String mapSave2(RequestBody MapString, Objects map, String username){System.out.println(map:map);System.out.println(username:username);return success;}
运行 点提交(报错) 总结无注解时什么都接收不了RequestParam注解时将参数包装成LinkedHashMap对象参数的key是Map的key参数的值是Map的valueget和
post请求都支持RequestBody注解接收json类型的数据(跟表单不一样表单传不了)也会包装成LinkedHashMap对象该注解不支持get请求get请求没有请求体不能传json 2.用对象接收map
(1)User类里加一个map
private MapString,Address userMap;
(2)前端
h3请求参数绑定封装到实体类存在map集合/h3form actionuser/save5.do methodpost姓名input typetext nameusername /br/年龄input typetext nameage /br/金额input typetext nameaddress.money /br/Map集合input typetext nameuserMap[one].money /br/Map集合input typetext nameuserMap[two].money /br/input typesubmit value提交 //form
(3运行 点提交 控制台 十、在控制器中使用原生的ServletAPI对象
只需要在控制器的方法参数定义HttpServletRequest和HttpServletResponse对象
UserController里加
/*原生的API*/
RequestMapping(/save6.do)
public String save6(HttpServletRequest request, HttpServletResponse response){System.out.println(request);// 获取到HttpSession对象HttpSession session request.getSession();System.out.println(session);System.out.println(response);return success;
}
运行 控制台