我知道有很多关于此的帖子,但我有2天测试所有(或可能是大多数)关于我的问题的答案,我继续遇到同样的问题。 我试图制作一个非常简单的webapp,它有一个index.jsp并导航到login.jsp 它们都有一些我在WebContent下包含的静态资源(比如css或png),所以我有:
@JsonBackReference
当我启动我的应用程序时,index.jsp显示完美(包括静态资源)但是当我通过单击index.jsp中的buttom导航到login.jsp时,资源没有被加载。
我已经看到当我导航时上下文根发生了变化,我确信这是问题,但我无法解决它。
感谢。
这是我的配置:
Web.xml中
WebContetn
|
|-----------resources (some subdirectories inside)
|
|-----------WEB-INF
|
|-----pages
|
|-----index.jsp
|-----login
|
|-----login.jsp
的applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>testSpringMVC</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springapp-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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="main.java.es.testSpringMVC.**" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
</beans>
主控制器
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
登录控制器:
@Controller
public class MainController {
@RequestMapping({ "/", "/index" })
public String showHomePage(Model model, HttpServletRequest request) {
return "index";
}
}
这里是jsps。
的index.jsp
@Controller
public class LoginSIDController {
@RequestMapping(value = "/login/loginSID")
public String loginSID() {
return "login/loginSID";
}
}
loginSID.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Index</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="shortcut icon" href="/resources/imagenes/favicon.ico" type="image/x-icon" />
<link href="resources/estilos/estilosmenu.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="cuerpo">
<img src="resources/imagenes/logo.png">
<br/>
<div class="botonera">
<a href="<c:url value='/login/loginSID'/>" class="boton" >SID</a>
</div>
</div>
</body>
</html>