This may have been asked many times but I have not been able to find a concrete answer over last two days. Using Spring 3.2.
I have two methods. One is for form creation and the other is for the form to post. Form creation works fine. But when I try to post/submit the form, I don’t see the log statement getting executed in the login() method and I get a 404 error HTTP Status 404 – /sample/login.
I think the problem is the form cannot submit to URL but I don`t know how to fix the mapping to make it work.
If there are any other files that are needed please let me know. BTW following example from sprin/mvc-basic and 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();
}
}
}
login.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>
name-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>
Your help appreciated.
Seems you have mapped
*.htmURL toDispatcherServlet(Guessing from/signup.htm). Change the action in your form tag tologin.htminstead oflogin: