我正在使用 Tiles 开展 Spring MVC 项目。当我运行app时,我没有得到任何错误。但是jsp页面没有正确加载,如下所示
当我查看此页面的源代码时,我可以看到layout.jsp和home.jsp代码已加载。但如上所示,该页面仅显示jsp标记。
开发环境
-> Spring MVC 3.2.8 Release
-> Apache Tiles 2.2.1
tiles-core-2.2.1.jar
tiles-template-2.2.1.jar
tiles-jsp-2.2.1.jar
tiles-servlet-2.2.1.jar
tiles-api-2.2.1.jar
tiles-request-api-1.0.1.jar
的pom.xml
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
的web.xml
<web-app version="2.5" 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">
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
tiles.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="base.definition"
template="/WEB-INF/views/templates/template1/layout.jsp">
<put-attribute name="title" value=""/>
<put-attribute name="header" value="/WEB-INF/views/templates/components/header.jsp"/>
<put-attribute name="menu" value="menu"/>
<put-attribute name="body" value=""/>
<put-attribute name="footer" value="/WEB-INF/views/templates/components/footer.jsp"/>
</definition>
<definition name="menu"
preparer="menuController"
template="/WEB-INF/views/templates/components/menu.jsp">
</definition>
<definition name="home" extends="base.definition">
<put-attribute name="title" value="Home"/>
<put-attribute name="body" value="/WEB-INF/views/home.jsp"/>
</definition>
layout.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title><tiles:getAsString name="title"/></title>
<link rel="stylesheet" type="text/css" href="<c:url
value='/resources/css/style.css'/>"/>
</head>
<body>
<div id="layout">
<div id="header">
<tiles:insertAttribute name="header" />
</div>
<div id="content">
<div id="leftContent">
<tiles:insertAttribute name="menu" />
</div>
<div id="rightContent">
<tiles:insertAttribute name="body" />
</div>
</div>
<div id="footer">
<tiles:insertAttribute name="footer" />
</div>
</div>
</body>
</html>
针对home.jsp
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<c:if test="${!empty kodListe}">
<h3 >En Son Eklenenler</h3>
<c:forEach items="${kodListe}" var="kod">
<form:form action="kod" method="POST">
<table>
<tr>
<td>
<input type="hidden" name="kodId" value="${kod.kodId}" />
<input value="${kod.kodBaslik}" type="submit"/>
</td>
</tr>
<tr>
<td>
${kod.kodEkleyenKullanici.kullaniciAd}
</td>
<td>
${kod.kodEklemeTarihi}
</td>
</tr>
</table>
</form:form>
</c:forEach>
servlet的context.xml中
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView">
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
如何解决问题?提前致谢
答案 0 :(得分:2)
问题是在web.xml中使用servlet映射的tile配置
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
当我使用/*
时, home.jsp 正确加载了图块配置,但没有css和图像。
在/
的情况下, home.jsp 不会加载切片配置,只会显示仅在home.jsp中定义的代码
我找到了解决方案here
为了解决问题作为使用tuckey进行url写入的Spring示例(https://src.springframework.org/svn/...ebapp/WEB-INF/),我添加了使用/*
url映射并更改 appServlet url模式的过滤器和过滤器映射到/
<强>的web.xml 强>
<!-- Reads request input using UTF-8 encoding -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
我从web.xml
<welcome-file-list>
<welcome-file>/WEB-INF/views/home.jsp</welcome-file>
</welcome-file-list>
最后我改变了 的 servlet的context.xml中强>
<!-- Forwards requests to the "/" resource to the "home" view -->
<mvc:view-controller path="/" view-name="home" />
然后一切都开始工作了。希望这有助于另一个stackoverflow用户