1.@RequestMapping
设置控制器类/请求处理方法的访问路径的
@RequestMapping可以作用在java类上,表示配置这个java类的访问路径;
package com.wangxing.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/hello")
public class HelloController{
@RequestMapping("/test1.do")
public ModelAndView testRequest(){
ModelAndView mav=new ModelAndView();
mav.addObject("info","hello,网星软件");
mav.setViewName("test.jsp");
return mav;
}
}
http://localhost:8080/spingmvc2/hello/test1.do
如果控制器类中没有@RequestMapping("/hello"),那么我们要访问请求处理方法就可以直接使用请求处理方法上@RequestMapping("/test1.do")的访问路径。
package com.wangxing.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController{
@RequestMapping("/test1.do")
public ModelAndView testRequest(){
ModelAndView mav=new ModelAndView();
mav.addObject("info","hello,网星软件");
mav.setViewName("test.jsp");
return mav;
}
}
http://localhost:8080/spingmvc2/test1.do
@RequestMapping也可以作用在请求处理方法上,表示配置这个请求处理方法的访问路径。
@RequestMapping的常用的属性:
1.value表示设置访问路径[可以省略]
@RequestMapping(value = "/test1.do") 可以省略
设置访问路径的时候可以设置通配符
? : 匹配任何单字符
例如:@RequestMapping("/?hello.test")
http://localhost:8080/spingmvc2/atest1.do
http://localhost:8080/spingmvc2/test1.do //错误
http://localhost:8080/spingmvc2/hhhtest1.do //错误
* : 匹配任意数量的字符
例如:@RequestMapping("/*hello.test")
http://localhost:8080/spingmvc2/test1.do
http://localhost:8080/spingmvc2/wtest1.do
http://localhost:8080/spingmvc2/wwwtest1.do
例如:@RequestMapping("/*/hello.test")
http://localhost:8080/spingmvc2/w/test1.do
http://localhost:8080/spingmvc2/www/test1.do
http://localhost:8080/spingmvc2/test1.do //错误
http://localhost:8080/spingmvc2/hhhh/www/test1.do //错误
** : 匹配多个路径
例如:@RequestMapping("/**/hello.test")
http://localhost:8080/spingmvc2/test1.do
http://localhost:8080/spingmvc2/w/test1.do
http://localhost:8080/spingmvc2/www/test1.do
http://localhost:8080/spingmvc2/hhh/www/test1.do
2.method--限制请求的访问方式【GET、POST.....】
表现形式:@RequestMapping(value = "/login.do",method = RequestMethod.POST )
Index.jsp
<%@page language="java" pageEncoding="UTF-8" %>
<html>
<body>
<form action="test1.do" method="get">
<input type="submit" value="测试Method属性"/>
</form>
</body>
</html>
package com.wangxing.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController{
@RequestMapping(value = "/test1.do",method = RequestMethod.POST)
public ModelAndView testRequest(){
ModelAndView mav=new ModelAndView();
mav.addObject("info","hello,网星软件");
mav.setViewName("test.jsp");
return mav;
}
}
将index.jsp页面中的表单提交放射修改成post即可成功。
只能处理get请求
@RequestMapping(value = "/gethello.do",method = RequestMethod.GET)
@GetMapping(value = "/gethello.do")
只能处理post请求
@RequestMapping(value = "/gethello.do",method = RequestMethod.POST)
@PostMapping(value = "/gethello.do")
2.请求处理方法接收请求参数值
1.@PathVariable 定义在方法上获取请求url路径上的参数数据
例如:
请求处理方法:
@RequestMapping(value = "/get1/{username}/{password}",method = RequestMethod.GET)
public ModelAndView getReqParam1(@PathVariable("username")String name,
@PathVariable("password")String pass){
ModelAndView mav=new ModelAndView();
mav.addObject("username",name);
mav.addObject("password",pass);
mav.setViewName("test.jsp");
return mav;
}
http请求:http://localhost:8080/spingmvc2/get1/zhangsan/123456
注意:web.xml文件的中央中央控制的<url-pattern>/</url-pattern>
2.@RequestParam 定义在方法上,获取请求中通过key=value方式传递的参数数据
例如:
请求处理方法
@RequestMapping(value = "/get2",method = RequestMethod.GET)
public ModelAndView getReqParam2(@RequestParam("username")String name, @RequestParam("password")String pass){
ModelAndView mav=new ModelAndView();
mav.addObject("username",name);
mav.addObject("password",pass);
mav.setViewName("test.jsp");
return mav;
}
http请求:http://localhost:8080/spingmvc2/get2?username=lisi&password=000000
web.xml文件的中央中央控制器的<url-pattern>*.do</url-pattern>
请求处理方法:@RequestMapping(value = "/get2.do",method = RequestMethod.GET)
http请求:http://localhost:8080/spingmvc2/get2.do?username=lisi&password=000000
3.HttpServletRequest对象的getParameter()方法接收数据
例如:
请求处理方法
@RequestMapping(value = "/get3.do",method = RequestMethod.GET)
public ModelAndView getReqParam3(HttpServletRequest request){
String name=request.getParameter("username");
String pass=request.getParameter("password");
ModelAndView mav=new ModelAndView();
mav.addObject("username",name);
mav.addObject("password",pass);
mav.setViewName("test.jsp");
return mav;
}
http请求:http://localhost:8080/spingmvc2/get3.do?username=wangwu&password=111111
4.在请求处理方法中定义对应参数变量,参数变量的名称与页面元素的name属性值相同
例如:
login.jsp
<%@page language="java" pageEncoding="UTF-8" isELIgnored="false" %>
<html>
<body>
<form action="get4.do" method="post">
用户名:<input type="text" name="username"/><br>
密码:<input type="password" name="password"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
请求处理方法:
@RequestMapping(value = "/get4.do",method = RequestMethod.POST)
public ModelAndView getReqParam4(String username,String password){
ModelAndView mav=new ModelAndView();
mav.addObject("username",username);
mav.addObject("password",password);
mav.setViewName("test.jsp");
return mav;
}
5.将需要提交的请求参数值封装到java对象中【java对象的成员变量的名称一定要与页面元素的name属性值相同】
例如: register.jsp
<%@page language="java" pageEncoding="UTF-8" isELIgnored="false" %>
<html>
<body>
<form action="get5.do" method="post">
用户名:<input type="text" name="username"/><br>
密码:<input type="password" name="password"/><br>
年龄:<input type="text" name="age"/><br>
地址:<input type="text" name="address"/><br>
日期:<input type="text" name="day"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
PersonBean.java
package com.wangxing.springmvc.bean;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class PersonBean {
private String username;
private String password;
private Integer age;
private String address;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date day;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getDay() {
return day;
}
public void setDay(Date day) {
this.day = day;
}
}
请求处理方法:
@RequestMapping(value = "/get5.do",method = RequestMethod.POST)
public ModelAndView getReqParam5(PersonBean personBean){
ModelAndView mav=new ModelAndView();
mav.addObject("personBean",personBean);
mav.setViewName("test.jsp");
return mav;
}
注意:java对象的成员变量的名称一定要与页面元素的name属性值相同
6.将被提交的请求参数组织成json数据【后面介绍】
SpringMVC访问静态资源
当我们使用SPringMVC访问HTML文件的时候出现404错误,因为SpringMVC不支持静态资源的访问【.html / .js / .css /.jpg.....】
方案一:激活Tomcat的defaultServlet来处理静态文件[web.xml]
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
要写在DispatcherServlet的前面, 让defaultServlet先拦截,这个就不会进入Spring了,我想性能是最好的吧。
方案二: 在spring3.0.4以后版本提供了mvc:resources
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />