SpringMVC请求参数的值

时间:2012-01-07 10:21:39

标签: java spring-mvc

我习惯了Java EE而不是Spring MVC。我刚刚创建了一个新的Spring MVC项目,在控制器中我生成了以下代码。

@Controller
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String login(Locale locale, Model model) {
        logger.info("Welcome home! the client locale is "+ locale.toString());


        return "home";
    }

}

通常情况下,我希望其中一个参数是请求和另一个响应,如何使用Spring MVC找到这些值?例如,我想捕获将在此处发送的用户名和密码?是否有类似于Spring MVC中的doGet和doPost方法的东西?

1 个答案:

答案 0 :(得分:1)

如果要捕获名为usernamepassword的参数,只需将它们声明为方法参数:

public String login(Locale locale, Model model, String username, String password) {

Spring将提供它们,或者如果它们不存在则抛出异常。

如果您选择:

,您可以同样声明HttpServletRequest参数
public String login(Locale locale, Model model, HttpServletRequest request) {

但第一种方法更清洁。