我正在开发一种简单的弹簧形式。但有些我如何得到404错误。
这是我的表格
<form:form method="post" action="login" modelAttribute="user" >
<form:label path="userName">User Name</form:label>
<form:input path="userName"/>
<form:label path="password">Password</form:label>
<form:password path="password"/>
<input type="submit" value="Login"/>
</form:form>
以上表格的我的控制器
@Controller
@RequestMapping(value="/login")
public class LoginController {
@RequestMapping(method = RequestMethod.POST)
public ModelAndView login(@ModelAttribute("user") User user){
if(user.getUserName().equals("raju") && user.getPassword().equals("pass")){
return new ModelAndView("success");
}
return new ModelAndView("failure");
}
}
当我提交表单时,控件不会进入LoginController。我的主页是http://localhost:8080/WebAppl
。当我提交表单时,网址为http://localhost:8080/login
知道我做错了吗?投入赞赏。
答案 0 :(得分:0)
请尝试此操作,看看错误是否仍然存在:
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String loginPageRenderer(@ModelAttribute("user") User user) {
return "login-page";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute("user") User user, Model model) {
if(user.getUserName().equals("raju") && user.getPassword().equals("pass")){
model.addAttribute("success", true);
return "success";
}
model.addAttribute("failure", true);
return "failure";
}
<form:form commandName="user" method="post">
<form:label path="userName">User Name</form:label>
<form:input path="userName"/>
<form:label path="password">Password</form:label>
<form:password path="password"/>
<input type="submit" value="Login"/>
</form:form>
答案 1 :(得分:0)
此错误404,如果找不到页面错误。您在xml文件中缺少路径内容路径。 这是web.xml的简单
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
And here is dispatcher-servlet.xml
<context:component-scan
base-package="com.sample" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:annotation-driven />