JAVA 框架-Springmvc
web.xml配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <? xml version="1.0" encoding="UTF-8"?> < web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- 配置spring-mvc文件 --> < servlet > < servlet-name >springmvc</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < init-param > <!-- 如果不写这个spring会自动去WEB-INF下找springmvc-servlet.xml文件 --> < param-name >contextConfigLocation</ param-name > <!-- 固定的名字,不能改变 --> < param-value >classpath:spring-mvc.xml</ param-value > </ init-param > < load-on-startup >1</ load-on-startup > <!-- 服务器启动时加载 --> </ servlet > < servlet-mapping > < servlet-name >springmvc</ servlet-name > < url-pattern >*.do</ url-pattern > <!--接收所有以.do结尾的请求--> </ servlet-mapping > <!--配置中文过滤器 --> < filter > < filter-name >characterEncodingFilter</ filter-name > < filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class > < init-param > < param-name >encoding</ param-name > < param-value >UTF-8</ param-value > </ init-param > </ filter > < filter-mapping > < filter-name >characterEncodingFilter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > </ web-app > |
spring-mvc.xml配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <? xml version="1.0" encoding="UTF-8"?> < beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> < context:component-scan base-package="com.hanqi.controller"></ context:component-scan > <!--注解扫描器--> < bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 视图解析器 --> < property name="prefix" value="/WEB-INF/page/"></ property > < property name="suffix" value=".jsp"></ property > </ bean > < mvc:annotation-driven > <!--配置spring消息转换器--> < mvc:message-converters register-defaults="false"> <!--不使用spring默认提供的转换器--> < bean class="org.springframework.http.converter.StringHttpMessageConverter"></ bean > < bean class= "org.springframework.http.converter.ByteArrayHttpMessageConverter"></ bean > < bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"></ bean > < bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> < property name="supportedMediaTypes"> <!-- 加入支持的媒体类型:返回contentType --> < list > < value >text/html;charset=utf-8</ value > <!-- 这里顺序不能反,一定先写text/html, 不然ie下会出现下载提示 --> < value >application/json;charset=utf-8</ value > </ list > </ property > </ bean > </ mvc:message-converters > </ mvc:annotation-driven > </ beans > |
Appuser实体类文件
AppuserController类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | package com.hanqi.controller; import static org.springframework.test.web.client.response.MockRestResponseCreators.withBadRequest; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.servlet.ModelAndView; import com.hanqi.model.Appuser; @SessionAttributes ( "orgUser" ) //该注解只能修饰类,当使用model设置属性时,会自动将model里属性名为orgUser的键值对放到session里 //可以传多个({"",""})model也需要设置多个 @Controller //声明当前类为控制器类 @RequestMapping ( "/appuser" ) //定义接收的请求 public class AppuserController { //参数传递 @RequestMapping ( "/login" ) //定义接受的请求 public String login( @RequestParam ( "uname" )String uname,String pword, @DateTimeFormat (pattern= "yyyy-MM-dd" )String birthday) { System.out.println( "用户名为" + uname); System.out.println( "密码为" + pword); System.out.println( "生日为" + birthday); return "index" ; } //实体传递,当传递带日期型参数的实体类时需要在类的成员变量上注解@DateTimeFormat(pattern="yyyy-MM-dd") //否则会报400错误 @RequestMapping ( "/register" ) public String register(Appuser user) { System.out.println( "用户名为" +user.getUname() ); System.out.println( "密码为" + user.getPword()); return "index" ; } //页面跳转 /*@RequestMapping(value="/{path}")//以路径作为请求参数 public String toPage(@PathVariable("path")String page) { return page; }*/ @ResponseBody //当向前台返回字符串时需要再加上这个注解 public String select() { return null ; } //使用ModelAndView //springmvc中,如果不设置视图的名字,会默认以请求路径作为视图名 @RequestMapping ( "login1" ) //defaultValue如果找不到会以此值为默认值,required默认为true,表示请求参数必须存在,可为空字符串但不能为null public ModelAndView login1( @RequestParam (value= "uname" ,defaultValue= "uname" ,required= false )String uname,String pword,ModelAndView mav) { Appuser user = new Appuser(); user.setUname(uname); user.setPword(pword); if ( "admin" .equals(uname)&& "123" .equals(pword)) { mav.addObject( "orgUser" , user); mav.setViewName( "index" ); } return mav; } //使用Model @RequestMapping (value= "login2" ,method={RequestMethod.POST}) //设置接收的请求为post请求,常用的还有get,delete,put,如果不匹配会报405错误 public String login2(String uname,String pword,Model model) { //同样也可以传入session,request对象进行属性设置 Appuser user = new Appuser(); user.setUname(uname); user.setPword(pword); if ( "admin" .equals(uname)&& "123" .equals(pword)) { model.addAttribute( "orgUser" , user); } return "index" ; } //在视图层可以拿到orgUser的值 } |
@ModelAttribute用法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package com.hanqi.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import com.hanqi.model.Appuser; @Controller @RequestMapping ( "/appuser" ) public class TestController { /*@ModelAttribute用法 1.单独修饰方法时,优先于其他方法执行,同时设置一个属性; 2.与requestMapping同时修饰时,请求路径作为视图层,返回值作为属性值放到request对象中; 3.修饰参数时,该参数不再接受请求,而是接收注解修饰方法中的参数*/ //第一种 @ModelAttribute ( "username" ) public String getUname(String uname) { System.out.println( "getUname: " + uname); // request.setAttribute("username", uname); return uname; } @RequestMapping ( "/login4" ) public String testAttribute1(String uname) { System.out.println( "testAttribute1: " + uname); return "index" ; } //第二种 @ModelAttribute ( "test" ) @RequestMapping ( "testModelAttribute2" ) public String testAttribute2(String uname) { System.out.println( "testAttribute2: " + uname); // request.setAttribute("test", "success"); return "success" ; } //第三种 @ModelAttribute ( "user" ) public Appuser getUser(Appuser user) { user.setPword( "1111" ); return user; } @RequestMapping ( "/login4" ) public String login4( @ModelAttribute ( "user" )Appuser user) { return "index" ; } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步