Spring3 MVC请求参数获取的几种方法

Spring3 MVC请求参数获取的几种方法

一、通过@PathVariabl获取路径中的参数

@RequestMapping(value="user/{id}/{name}",method=RequestMethod.GET)

public String test (@PathVariable String id,@PathVariable String name, ModelMap model) {   

        System.out.println(id);

        System.out.println(name);

        model.addAttribute("message", "111111");

        return "users";

    }

二、@ModelAttribute获取POST请求的FORM表单数据

JSP表单如下

<form method="post" action="hao.do">

    a: <input id="a" type="text"   name="a"/>

    b: <input id="b" type="text"   name="b"/>

    <input type="submit" value="Submit" />

 </form>

JavaBean如下

    public class Pojo{

        private String a;

        private int b;

    }

Java Controller如下

@RequestMapping(method = RequestMethod.POST)

public String processSubmit(@ModelAttribute("pojo") Pojo pojo) {   

    return "helloWorld";

}

三、直接用HttpServletRequest获取

@RequestMapping(method = RequestMethod.GET)

public String get(HttpServletRequest request, HttpServletResponse response) {

   System.out.println(request.getParameter("a"));

    return "helloWorld";

}

四、用注解@RequestParam绑定请求参数

用注解@RequestParam绑定请求参数a到变量a

当请求参数a不存在时会有异常发生,可以通过设置属性required=false解决,

例如: @RequestParam(value="a", required=false)

Controller如下

@RequestMapping(value = "/requestParam", method = RequestMethod.GET)

public String setupForm(@RequestParam("a") String a, ModelMap model) {

   System.out.println(a);

return "helloWorld";

}

摘自leiOOlei博客http://www.cnblogs.com/leiOOlei/p/3658147.html

自己使用体会

1. 当表单和JavaBean不一一对应时候,使用注解@RequestParam绑定请求参数,

如果参数名和前台的一样可简写

JSP页面代码:

<form action="login/check" method="post">

    <h2>欢迎你</h2>

        <label>帐号</label>

        <input type="text" id="username" name="username" class="username">

        </br>

<label>密码</label>

        <input type="password" id="password" name="password" class="password">

        </br>

<input name="login" type="submit" value="登录"/>

</form>

 

Controller代码:

@RequestMapping(value="check",method=RequestMethod.POST)

public ModelAndView check (HttpServletRequest request, String username,String password) {

//养成转码的好习惯

        username = new String(username.getBytes("iso8859-1"),"UTF-8");

        System.out.println("----------->"+username);

        System.out.println("----------->"+password);

        return new ModelAndView("/welcome");       

}

 

2. 当表单和JavaBean一一对应时候,使用@ModelAttribute获取POST请求的FORM表单数据

JSP页面代码:

<form action="login/check" method="post">

    <h2>欢迎你</h2>

        <label>帐号</label>

        <input type="text" id="username" name="username" class="username">

        </br>

<label>密码</label>

        <input type="password" id="password" name="password" class="password">

        </br>

<input name="login" type="submit" value="登录"/>

</form>

Controller代码:

@RequestMapping(value="check",method=RequestMethod.POST)

public ModelAndView check(HttpServletRequest request, User user) {

        System.out.println("----------->"+user.getUsername());

        System.out.println("----------->"+user.getPassword());

        return new ModelAndView("/welcome");       

}

JavaBean代码:

public class User {

    private String username;

private String password;

public String getUsername() {

        return username;

    }

    public void setUsername(String username) {

        this.username = username == null ? null : username.trim();

    }

    public String getPassword() {

        return password;

    }

    public void setPassword(String password) {

        this.password = password == null ? null : password.trim();

}

}

 


你可能感兴趣的:(Spring3 MVC请求参数获取的几种方法)