开发工具:IDEA
一:什么是springMVC
springMVC和我们使用的struts2一样都是用来写页面跳转的
二:如何搭建SpringMVC框架(Maven)
(1)、导入jar包--加入spring-webmvc依赖
<!--springmvc依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.3.RELEASE</version> </dependency>
(2)、配置web.xml配置文件
<!--配置扫描器--> <context:component-scan base-package="com.controller"></context:component-scan> <!--视图解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--前缀--> <property name="prefix" value="前缀路径(/)"></property> <!--后缀--> <property name="suffix" value="后缀(.jsp)"></property> </bean>
(3)、新建控制器类
类:@Controller 方法:@RequestMapping("/请求路径")
三:注解
(1)、@Controller--用来注解类的
(2)、@RequestMapping("/请求路径")
修饰类:jsp需要在请求路径前面加上修饰类的路径
修饰方法:jsp请求路径直接写在修饰方法的路径
参数列表:value--请求路径(默认属性可以不写)
method--请求方法 RequestMethod.xxx
params--请求参数
heads--请求头
通配符:?--匹配路径中的一个字符
*--匹配路径中的任意字符
**--匹配多层路径
(3)、@RequestParams--获取请求参数值
作用在方法参数中
参数列表:value--参数名 required--是否必须 defaultValue--默认值
(4)、@RequestHeader--映射请求头
(5)、@@CookieValue--映射cookie值
(6)、@ModelAttribute--适用场所为修改数据,作用在方法上,目的为查找对象
ps:当我们在修改界面数据时,有时候我们不一定需要全部修改,只需要修改部分内容,那么其他的值就没有必要绑定出来了,只要绑定我们需要修改的值就可以了,但是如果直接这样通过对象得到的话,你没有绑定得值会得不到,这时我们就需要用@ModelAttribute注解来解决了。
代码:
//修改数据(先通过id得到对象,然后在绑定对象进行修改) Person person; //通过id得到对象 @RequestMapping("getPersonID") public String getPersonID(Map map){ //传递接收id,根据id查询数据库的值 person = new Person("admin","123"); System.out.println("数据库中查询出来的值为:"+person); map.put("person",person); return "update"; } @ModelAttribute public void getperson(Map map){ map.put("person",person); System.out.println("重新赋值"); }
//修改 @RequestMapping("updateperson") public String updateperson(Person person){ System.out.println("修改之后:"+person); return "redirect:/success.jsp"; }
代码如下:
@RequestMapping(value = "**/say/{id}",method = RequestMethod.GET) //?一级,*一级,**两级 public String say(@PathVariable("id") int id){ System.out.println("进来了"); return "success"; } @RequestMapping(value = "b",method = RequestMethod.POST) public String b(){ System.out.println("进入b"); return "b"; } @RequestMapping("login") public String login(@RequestParam(value = "uname",required = true) String uname, @RequestParam(value = "upwd",required = false,defaultValue ="222") String upwd){ // System.out.println(uname+" "+upwd); return "success"; }
四:使用POJO对象绑定请求参数值
controller类
//通过对象登录(通过servlet依赖) @RequestMapping("loginperson") public String loginperson(Person person){ //会自动通过对象得到值 System.out.println("输入的内容"+person); return "success"; }
jsp页面:
<form action="/loginperson.action" method="post"> 用户名:<input type="text" name="uname"><br/><br/> 密 码:<input type="text" name="upwd"><br/> <input type="submit" value="登录"> </form>
五:数据保存到作用域中(三种方法--servlet依赖得到request/Map/ModelAndView)
依赖:
<!--servlet依赖--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version> <scope>provided</scope> </dependency>
controller类:
//通过servlet依赖
@RequestMapping("loginperson") public String loginperson(Person person, HttpServletRequest request){ //request request.setAttribute("person",person); //session //request.getSession().setAttribute("person",person); //application // request.getSession().getServletContext(); System.out.println("输入的内容"+person); return "success"; }
//后面两种方法只能得到request,如果想得到session中的话需要使用
@SessionAttributes("p")注解,加在类上方
//(通过Map) @RequestMapping("loginperson") public String loginperson(Person person,Map map){ map.put("p",person); System.out.println("输入的内容"+person); return "success"; }
//(通过ModleAndView) @RequestMapping("loginperson") public ModelAndView loginperson(Person person){ ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("p",person); modelAndView.setViewName("success"); System.out.println("输入的内容"+person); return modelAndView; }
六:自定义转换器
1、自定义实现接口converter,添加注解@Component
2、在springmvc.xml配置文件中配置转换器:
<!--引入自定义转换器--> <mvc:annotation-driven conversion-service="mypoint"></mvc:annotation-driven> <!--加载自定义转换器--> <bean id="mypoint" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="转换器类"></bean> </list> </property> </bean>