3 回答
TA贡献1848条经验 获得超6个赞
尝试以下操作:
更改
@RequestMapping(value="/login.htm")
为@RequestMapping(value="/login")
更改
<a href="login.htm">Login</a>
为<a href="/SpringWebApp/login">Login</a>
看起来您提供的链接不正确,因此您的控制器没有接收到它。
TA贡献1828条经验 获得超3个赞
在你的web.xml你还没有定义spring-servlet.xml. 所以像这样定义spring-servlet.xml。web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>SpringWebApp</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-servlet.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
现在写下你的spring-servlet.xml喜欢
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="pac.test" />
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
像这样写你的控制器
@Controller
public class LoginController {
@RequestMapping(value="/")
public ModelAndView loginRequest() {
ModelAndView mv = new ModelAndView("home");
return mv;
}
}
而在index.jsp页面中,而不是 <a href="login.htm">Login</a>像这样写重写 <a href="/SpringWebApp/">Login</a>
TA贡献1831条经验 获得超9个赞
@RequestMapping(value="/login.htm")
public ModelAndView loginRequest() {
ModelAndView mv = new ModelAndView("home");
return mv;
}
像这样从“/login.htm”中删除“.htm”
@RequestMapping(value="/login")
public ModelAndView loginRequest() {
ModelAndView mv = new ModelAndView("home");
return mv;
}
并像这样更正您的超链接
<h2>
<a href="/login">Login</a>
</h2>
添加回答
举报