【问题标题】:Handling form submission in a jsp page in Spring MVC在 Spring MVC 中处理 jsp 页面中的表单提交
【发布时间】:2013-10-09 10:55:50
【问题描述】:

我正在开发一个 Spring MVC 项目,我正在尝试提交一个表单并在同一个 jsp 页面中处理它,以便对用户进行身份验证。

我确实通过使用 ajax 请求调用内置于 Controller 中的 Web 服务对用户进行了身份验证,但为了在这次不使用 Ajax 的情况下做到这一点,我收到了这个错误 Request method 'POST' not supported

这是我的代码:

表格

<form action="/gethealthy/isuser" method="post">
  <input type="text" name="username" placeholder="Username" />
  <input type="password" name="password" placeholder="Password" />
  <button type="submit">Sign In</button>
</form>

JSP 代码

if (request.getParameter("username") != null) {  
      HomeController aHomeController = new HomeController();
      String username = request.getParameter("username");
      String password = request.getParameter("password");
      String result = aHomeController.isUser(username, password);
      if (aHomeController.isUser(username, password)) {
        String redirectURL = "project/dashboard";
        response.sendRedirect(redirectURL);
      } else {
            out.print("Wrong credentials");
      }

【问题讨论】:

    标签: java spring jsp spring-mvc


    【解决方案1】:

    你必须像这样映射你的控制器:

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

    【讨论】:

    • 但我告诉过你,我不想访问控制器。是否可以在视图中做到这一点?
    • 我知道我知道,但我有一个问题,我想我无法以其他方式解决它。我期望 3 个不同的结果会执行不同的操作。
    • 但无论如何,谢谢,我想出了一种方法来做我想做的事情:D。我使用我的 Web 服务使用 GET 参数进行响应,然后将根据这些参数执行操作。
    猜你喜欢
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多