0

这可能已被问过很多次,但在过去的两天里我一直无法找到具体的答案。使用 Spring 3.2。

我有两种方法。一种用于创建表单,另一种用于发布表单。表单创建工作正常。但是当我尝试发布/提交表单时,我看不到日志语句在该login()方法中被执行,并且我收到一个 404 错误 HTTP Status 404 - /sample/login.

我认为问题是表单无法提交到 URL,但我不知道如何修复映射以使其工作。

如果还有其他需要的文件,请告诉我。BTW 以下示例来自 sprin/mvc-basic 和 spring/petclinic

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import example.domain.User;
import example.service.UserValidator;


@Controller
@RequestMapping(value="/login")
public class LoginFormController {  

   protected final Log logger = LogFactory.getLog(getClass());

   @RequestMapping(method=RequestMethod.GET)
   public String loadForm(Model model) {
        logger.info("LoginFormController login");
        model.addAttribute("user", User.getUserInstance());
        return "login";
   }

   @RequestMapping(method=RequestMethod.POST)
   public String login(@ModelAttribute User user, BindingResult result) {
        logger.info("post");
        new UserValidator().validate(user, result);
        if (result.hasErrors()) {
            return "login";
         } else {
            logger.info("Email Id: " + user.getEmailId());
            //this.clinic.storeOwner(owner);
            //status.setComplete();
            return "redirect:/landing/" + user.getEmailId();
         }
   }
}

登录.jsp

    <%@ include file="/WEB-INF/jsp/include.jsp"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

    <html>
    <head>
    <title><fmt:message key="title" /></title>
    <style>
    .error {
        color: red;
    }
    </style>
    </head>
    <body>
        <h1>
            <fmt:message key="login.heading" />
        </h1>
        <form:form method="post" modelAttribute="user" action="login">
            <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5">
                <tr>
                    <td align="right" width="20%"><form:label for="emailId" path="emailId" cssErrorClass="error">Email ID:</form:label></td>
                    <td width="20%"><form:input path="emailId" /></td>
                    <td width="60%"><form:errors path="emailId" cssClass="error" /></td>
                </tr>
                <tr>
                    <td align="right" width="20%"><form:label for="password" path="password" cssErrorClass="error">Password:</form:label></td>
                    <td width="20%"><form:input path="password" /></td>
                    <td width="60%"><form:errors path="password" cssClass="error" /></td>
                </tr>
            </table>
            <br>
            <input type="submit" align="center" value="Login">
        </form:form>
        <a href="<c:url value="signup.htm"/>">Signup</a>
    </body>
    </html>

名称-servlet.xml

<bean....>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
   </bean>

 </bean>

感谢您的帮助。

4

1 回答 1

2

似乎您已将*.htmURL 映射到DispatcherServlet(从 猜测/signup.htm)。将表单标记中的操作更改为,login.htm而不是login

<form:form method="post" modelAttribute="user" action="login.htm">
于 2012-12-18T09:52:28.507 回答