原创

Spring MVC之Controller接受请求参数的常见方式

1、通过实体bean接收请求参数
通过一个实体bean来接收请求参数,适用于get和post提交请求方式。需要注意的是,bean的属性名称必须与请求参数名称相同。

package controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import domain.UserForm;
@Controller
@RequestMapping("/user")
public class UserController{
    @RequestMapping("/register")
    public String register(UserForm user, Model model){
        if("zhangsan".equals(user.getUname())
                &&"123456".equals(user.getUpass()){
            return "login"; //注册成功,跳转到login.jsp
        }else{
            //在register.jsp页面上使用EL表达式$(user.uname)取出model的uname值
            model.addAttribute("uanme", user.getUname());
            return "register";//返回 register.jsp
        }
    }
}

2、通过处理方法的形参接收请求参数

package controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController{
    @RequestMapping("/register")
    //必须加method属性
    //通过形参接收请求参数,形参名称与请求参数名称完全相同
    public String register(String uname, String upass, Model model){
        if("zhangsan".equals(uname)
                &&"123456".equals(upass){
            return "login"; //注册成功,跳转到login.jsp
        }else{
            //在register.jsp页面上使用EL表达式$(user.uname)取出model的uname值
            model.addAttribute("uanme");
            return "register";//返回 register.jsp
        }
    }
}

3、通过HttpServletRequest接受请求参数

package controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController{
    @RequestMapping("/register")
    //必须加method属性
    public String register(HttpServletRequest request, Model model){
        String uname = request.getParameter("uname");
        String upass = request.getParameter("upass");
        if("zhangsan".equals(uname)
                &&"123456".equals(upass){
            return "login"; //注册成功,跳转到login.jsp
        }else{
            //在register.jsp页面上使用EL表达式$(user.uname)取出model的uname值
            model.addAttribute("uanme");
            return "register";//返回 register.jsp
        }
    }
}

4、通过@PathVariable接收URL中的请求参数

package controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/user")
public class UserController{
    @RequestMapping(value="/register/{uname}/{upass}",method=RequestMethod.GET)
    //必须加method属性
    public String register(@PathVariable String uname, @PathVariable String upass, Model model){
        if("zhangsan".equals(uname)
                &&"123456".equals(upass){
            return "login"; //注册成功,跳转到login.jsp
        }else{
            //在register.jsp页面上使用EL表达式$(user.uname)取出model的uname值
            model.addAttribute("uanme");
            return "register";//返回 register.jsp
        }
    }
}

访问http://localhost:8080/ch2/user/register/zhansan/123456路径时,上述代码自动将URL中的模板变量{uname}和{upass}绑定到通过@Path Variable注解的同名参数上,即uname=zhangsan、upass=123456

5、通过@RequestParam接收请求参数

package controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/user")
public class UserController{
    @RequestMapping("/register")
    public String register(@RequestParam String uname, @RequestParam String upass, Model model){
        if("zhangsan".equals(uname)
                &&"123456".equals(upass){
            return "login"; //注册成功,跳转到login.jsp
        }else{
            //在register.jsp页面上使用EL表达式$(user.uname)取出model的uname值
            model.addAttribute("uanme");
            return "register";//返回 register.jsp
        }
    }
}

6、通过@ModelAttribute接收请求参数
@ModelAttribute注解放在处理方法的形参上时,用于将多个请求参数封装到一个实体对象,从而简化数据绑定流程,而且自动暴露为模型数据,于视图页面时使用。bean只是将多个请求参数封装到一个实体对象,并不能暴露为模型数据(需要使用model.addAttribute语句才能暴露为模型数据。)

package controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import domain.UserForm;
@Controller
@RequestMapping("/user")
public class UserController{
    @RequestMapping("/register")
    public String register(@ModelAttribute("user") UserForm user){
        if("zhangsan".equals(user.getUname()
                &&"123456".equals(user.getUpass()){
            return "login"; //注册成功,跳转到login.jsp
        }else{
            //使用@ModelAttribute("user")与model.addAttribute("user", user)相同
            //在register.jsp页面上使用EL表达式$(user.uname)取出ModelAttribute的uname值
            return "register";//返回 register.jsp
        }
    }
}

参考资料:
https://blog.csdn.net/qq_25819725/article/details/79873940

正文到此结束
本文目录