2014-07-22 72 views
1

我正在使用瓷磚一個Spring MVC的項目。當我運行的應用程序,我沒有得到任何error.But jsp頁面加載不正確像下面Spring MVC的瓷磚jsp頁面中正確載入

enter image description here

當我看到此頁面的源代碼時,我可以看到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> 

如何解決這個問題呢?在此先感謝

回答

2

的問題是瓷磚在web.xml

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

與servlet映射配置當我使用/*回到Home.jsp正確加載塊配置,但沒有CSS和圖像。

在這種情況下/針對home.jsp不加載塊配置並且示出了僅將其僅在針對home.jsp

我發現溶液here 爲了解決該問題爲一個Spring定義的代碼使用tuckey做URL寫入(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" /> 

然後,一切都開始work.Hope這幫助另一計算器用戶