SpringMVC controller的参数和跳转
1 返回值
常见返回值是:String
其它类型:ModelAndView, Model, View, 以及其他任意java类型
package com.w.controller;
import com.w.domain.UserInfo;
import com.w.formatter.MyDateFormatter;
import com.w.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.List;
/**
* @ClassNameUserController
* @Description
* @Author ANGLE0
* @Date2019/10/18 13:28
* @Version V1.0
**/
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
//返回值为String,跳转到页面
@RequestMapping("/active.do")
public String activeUser(String activeCode){
int active = userService.active(activeCode);
if (active > 0) {
return "ok";
}
return "error";
}
//返回值为ModelAndView,保存数据对象,同时设置跳转页面
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
ModelAndView modelAndView=new ModelAndView();
List<UserInfo> userInfos = userService.findAll();
modelAndView.addObject("users",userInfos);
modelAndView.setViewName("userList");
return modelAndView;
}
//返回json
@RequestBody
@RequestMapping("/findOne.do")
public String findOne(){
List<UserInfo> userInfo = userService.findOne();
return userInfo;
}
}
2 参数
常见模式:
Object param:
例如:String name
HttpServletRequest
Model
@PathVariable:抽取url中的请求参数
@RequestParam:适用于post和get
@ModelAttribute:
多个参数分装为一个实体,并暴露为模型数据,而直接使用 (Object object)为参数,需要暴露为一个模型数据时需要使用model.addAttribute
被此注解注解的方法,在每次调用控制类前被调用
package com.w.controller;
import com.w.domain.UserInfo;
import com.w.formatter.MyDateFormatter;
import com.w.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.List;
/**
* @ClassNameUserController
* @Description
* @Author ANGLE0
* @Date2019/10/18 13:28
* @Version V1.0
**/
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
//参数为 String, 参数名为 activeCode
//直接取值即可
@RequestMapping("/active.do")
public String activeUser(String activeCode){
int active = userService.active(activeCode);
if (active > 0) {
return "ok";
}
return "error";
}
//无参
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
ModelAndView modelAndView=new ModelAndView();
List<UserInfo> userInfos = userService.findAll();
modelAndView.addObject("users",userInfos);
modelAndView.setViewName("userList");
return modelAndView;
}
//参数:Httpsession, HttpServletRequest
@RequestMapping("/findOne.do")
public String findOne(Httpsession session,HttpServletRequest request){
int a = 1;
session.setAttribute("nub", a);
request.setAttribute("nu", a);
return "login";
}
//参数:Model
@RequestMapping("/findOne.do")
public String findOne(Model model){
model.setAttribute("success", "成功");
return "login";
}
//参数:@ModelAttribute
//将参数分装为uer中
//同时创建User对象实例,并以键为("userForm")值为uesr的方式存放到Model中
@RequestMapping("/findOne.do")
public String findOne(@ModelAttribute UserForm user){
model.setAttribute("success", "成功");
return "login";
}
//参数:@ModelAttribute("user")
//将参数分装为uer中
//同时创建User对象实例,并以键为("user")值为uesr1的方式存放到Model中
@RequestMapping("/findOne.do")
public String findOne(@ModelAttribute("user") User user1){
model.setAttribute("success", "成功");
return "login";
}
}
3 重定向和转发
package com.w.controller;
import com.w.domain.UserInfo;
import com.w.formatter.MyDateFormatter;
import com.w.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.List;
/**
* @ClassNameUserController
* @Description
* @Author ANGLE0
* @Date2019/10/18 13:28
* @Version V1.0
**/
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/active.do")
public String activeUser(String activeCode){
//重定向到一个方法
return "redirect:/index";
}
@RequestMapping("/active1.do")
public String activeUser1(String activeCode){
//转发到一个方法,当在一个控制类中时可以省略相同的部分
return "forward:/index";
}
@RequestMapping("/active2.do")
public String activeUser2(String activeCode){
//转发到一个视图
return "index";
}
}