SpringMVC controller的参数和跳转

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";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值