struts2+spring4+hiobernate4整合sitemesh导致jsp页面引入的js失效解决方案

1.查看web.xml文件里的sitemesh过滤器的配置<span style="color:#ff0000;"> 注意顺序的问题</span>
<?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" id="WebApp_ID" version="3.0">
  <display-name>ssh_user</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:beans.xml</param-value>
  </context-param>
  <filter>
    <filter-name>CharacterFilter</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>
  </filter>
  <filter-mapping>
    <filter-name>CharacterFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>LoginFilter</filter-name>
    <filter-class>com.han.util.LoginFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>LoginFilter</filter-name>
    <url-pattern>/user/*</url-pattern>
    <url-pattern>/message/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>SystemContextFilter</filter-name>
    <filter-class>com.han.util.SystemContextFilter</filter-class>
    <init-param>
      <param-name>pageSize</param-name>
      <param-value>15</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>SystemContextFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>openSessionInViewerFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>openSessionInViewerFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
    <filter>
        <filter-name>struts-prepare</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
    </filter>

  <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
    </filter>

    <filter>
        <filter-name>struts-execute</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts-prepare</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>

    <filter-mapping>
        <filter-name>struts-execute</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>
    </listener></span>
</web-app>

2.注意decorators.xml和所要装饰要引入的jsp页面的位置


3.注意decorators.xml里的配置

<?xml version="1.0" encoding="UTF-8"?>

<span style="color:#ff6666;"><decorators defaultdir="/WEB-INF/decorators"> <!--配置要引入的jsp页面的文件路径 --></span>
    <!-- Any urls that are excluded will never be decorated by Sitemesh -->
    <!-- exclude中可以定义那些页面不使用模板 -->
   <excludes>
        <pattern>/exclude.jsp</pattern>
        <pattern>/exclude/*</pattern>
        <pattern>/02.jsp</pattern>
    </excludes>

    <decorator name="basic" page="top.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>
4.注意jsp页面里的写法
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> <span style="color:#ff0000;"><!--要引入的decorator的标签 --></span>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title><decorator:title/></title>
   <span style="color:#ff0000;"> <!--所要引入的公共的js和css样式 必须放在head标签里面,否则不起作用 --></span>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.8.3.js"></script>
   <span style="color:#ff0000;"> <decorator:head/><!-这里要注意,我一开始自己练习的时候,这里没有加入,导致jsp页面引入的js失效,花费我一天的时间去找错 --></span>
  </head>
  <body>
  <!--这里可以写一一些需要加入的页面内容 -->
   <decorator:body/><span style="color:#ff0000;"><!--加载jsp页面的主体部分 --></span>
<pre name="code" class="html">   <!--这里可以写一一些需要加入的页面内容 -->
</body></html>


5.最终显示结果


这也只是个人做项目时所遇到的问题,只是提供参考,谢谢支持!


展开阅读全文

没有更多推荐了,返回首页