1@Controller、@RequestMapping、@RequestParam注解
1.1@Controller
@Controller 注解用于声明某类的实例是一个控制器,
@Controller
public class UserController {
//业务代码
}
注意:需要通过SpringMVC通过扫描机制,来找到应用中基于注解的控制器类.为了让控制器类被 Spring MVC 框架扫描到,需要在配置文件中声明 spring-context,并使用 <context:component-scan/> 元素指定控制器类的基本包(请确保所有控制器类都在基本包及其子包下)
<context:component-scan base-package="com.nie.controller" />
1.2@RequestMapping
@RequestMapping 用于建立请求 URL 和处理请求方法之间的对应关系,负责将请求映射到对应的控制器方法上。
@RequestMapping可以用于类和方法上,用于类上,表示类中的所有响应请求的方法都以该地址作为父路径。
常用属性:
value:value 属性是用于指定请求的URL, @RequestMapping 注解的默认属性,因此如果只有 value 属性时,可以省略该属性名,如果有其它属性,则必须写上 value 属性名称,它和path属性的作用是一样的。
method:method 属性用于表示该方法支持哪些 HTTP 请求。如果省略 method 属性,则说明该方法支持全部的 HTTP 请求。
params:params 属性用于指定限制请求参数的条件。它支持简单的表达式。要求请求参数的key和value必须和配置的一模一样,
@Controller
public class UserController {
@RequestMapping(value = "/getUser",method = RequestMethod.GET,params ={ "name","age"})
public String getUer(){
System.out.println("getUser.....");
return "/index.jsp";
}
}
详细属性通过该源码查看
1.3@RequestParam
- value:与请求参数名称
- required:此在指定的请求参数是否必须包括,默认是true,提交时如果没有此参数则报错
- defaultValue:当没有指定请求参数时,则使用指定的默认值赋值
@RequestMapping(value = "/getUser",method = RequestMethod.GET)
@ResponseBody
public void getUer(@RequestParam(value = "name",required = false) String username, @RequestParam(defaultValue = "18") int age ){
System.out.println(username);
System.out.println(age);
}
2数据请求响应方式-请求数据
SpringMVC可以接收的请求参数为
- 基本类型参数
- POJO类型参数
- 数组类型参数
- 集合类型参数
①基本类型参数
http://localhost:8080/springMVC/getUser?username=zhangsan&age=12
@RequestMapping(value = "/getUser",method = RequestMethod.GET)
@ResponseBody
public void getUer(String username,int age){
System.out.println(username);
System.out.println(age);
}
②POJO类型参数
http://localhost:8080/springMVC/getUser?username=zhangsan&age=12
public class User {
private String username;
private int age;
getter/setter…
}
@RequestMapping("/getUser")
@ResponseBody
public void getUser(User user) throws IOException {
System.out.println(user);
}
③数组类型参数
//请求地址
http://localhost:8080/springMVC/getUser?user1=zhangsan&user1=lisi
//controller方法
@RequestMapping("/getUser")
@ResponseBody
public void getUser(String[] users) throws IOException {
System.out.println(Arrays.asList(users));
}
④集合数据类型
封装到实体类或者直接通过List接收
封装到实体类
--前端代码< form action= " ${ pageContext.request.contextPath } /getUser" method= "post" >< input type= "text" name= "userList[0].username" >< br >< input type= "text" name= "userList[0].age" >< br >< input type= "text" name= "userList[1].username" >< br >< input type= "text" name= "userList[1].age" >< br >< input type= "submit" value= " 提交 " >< br ></form>
--Controller层代码
@RequestMapping ( "/saveUser" )@ResponseBodypublic void saveUser (Vo vo) throws IOException {System. out .println(vo.getUserList());}
--实体类
public class VO {
private List<User> userList;
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
@Override
public String toString() {
return "VO{" +
"userList=" + userList +
'}';
}
}
public class User {
private String username;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", age=" + age +
'}';
}
}
通过List接收:
当使用ajax提交时,可以指定contentType为json形式,那么在方法参数位置使用@RequestBody可以直接接收集合数据而无需使用POJO进行包装。
< script >// 模拟数据var userList = new Array ();userList . push ({ username : "zhangsan" , age : "20" });userList . push ({ username : "lisi" , age : "20" });$ . ajax ({type : "POST" ,url : "/springMVC/saveUser" ,data : JSON . stringify ( userList ),contentType : 'application/json;charset=utf-8'});</ script >
@RequestMapping("/saveUser")
@ResponseBody
public void saveUser(@RequestBody List<User> userList) throws
IOException {
System.out.println(userList);
}
3数据响应方式-响应数据
3.1返回页面跳转
直接返回字符串:此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转。
比如返回index,会根据spring-mvc.xml配置文件拼接转发到资源地址/jsp/index.jsp
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- /jsp/xxx.jsp -->
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
controller方法为
//页面跳转 返回字符串
@RequestMapping(value = "test01",method = RequestMethod.GET,params = {"value1","value2"})
public String test01(){
System.out.println("返回字符串测试");
return "index";
}
返回ModelAndView:
/*页面跳转返回ModelAndView
Model:模型 作用封装数据
View:视图 作用展示数据
*/
@RequestMapping(value="/test02")
public ModelAndView test02(){
ModelAndView modelAndView = new ModelAndView();
//设置模型数据
modelAndView.addObject("testkey","testvalue");
//设置视图名称
modelAndView.setViewName("test02");// /jsp/test02.jsp
return modelAndView;
}
3.2页面回写数据
注入的response对象
通过SpringMVC框架注入的response对象,使用response.getWriter().print(“hello world”) 回写数据,此时不需要视图跳转,业务方法返回值为void。、
@RequestMapping(value="/test")
public void test(HttpServletResponse response) throws IOException {
response.getWriter().print("test springmvc");
}
通过@ResponseBody注解(常用)
将需要回写的字符串直接返回,但此时需要通过@ResponseBody注解告知SpringMVC框架,方法返回的字符串不是跳转是直接在http响应体中返回。
@RequestMapping(value="/test")
@ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
public String test() throws IOException {
return "test springmvc";
}