我是Spring MVC的新手。 请原谅这是一个愚蠢的问题,我自己尝试了很多,但无法解决
我面临的问题是我的控制器永远不会被称为
这是代码
的index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="/hello.html" method="post">
Name:<input type="text" name="name"/><br/>
Password:<input type="password" name="password"/><br/>
<input type="submit" value="login"/>
</form>
</body>
</html>
dispatcher-servlet.xml
<context:component-scan base-package="com.javatpoint" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
hellopage.jsp
Message is: ${message}
的errorPage.jsp
${message}
<jsp:include page="/index.jsp"></jsp:include>
HelloWorldController.java
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloWorld(HttpServletRequest request,HttpServletResponse res) {
String name=request.getParameter("name");
String password=request.getParameter("password");
System.out.println("The Name is"+name);
System.out.println("The password is"+password);
if(password.equals("admin")){
String message = "HELLO "+name;
return new ModelAndView("hellopage", "message", message);
}
else{
return new ModelAndView("errorpage", "message","Sorry, username or password error");
}
}
}
的web.xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:1)
在表单操作中包含您的应用上下文路径:
<form action="${pageContext.request.contextPath}/hello.html" method="post">
答案 1 :(得分:0)
尝试将表单的操作从/hello.html
更改为/hello
,因为您在控制器中拥有的内容@RequestMapping