现在的位置: 首页 > 综合 > 正文

Struts2与URLRewrite的集成

2019年06月19日 ⁄ 综合 ⁄ 共 6006字 ⁄ 字号 评论关闭

1、struts2的版本要高于2.1.8.1

2、需要的jar包有:struts.jar,struts2-convention-plugin-2.1.8.1.jar,struts2-core-2.1.8.1.jar,struts2-dojo-plugin-2.1.8.1.jar,struts2-embeddedjsp-plugin-2.1.8.1.jar,struts2-spring-plugin-2.1.8.1.jar,xwork-core-2.1.6.jar,urlrewrite-3.2.0.jar,aspectjweaver.jar,aspectjrt.jar,aspectjlib.jar,asm-util-2.2.3.jar,asm-commons-2.2.3.jar

3、需要替换struts2 listener:org.apache.struts2.dispatcher.FilterDispatcher 为:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
并为其添加如下参数:

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

4、web.xml的编写:
    a,请保持与struts2对web.xml的配置建议,即:servlet,filter,listener,..
    b,其中listener:
        1、org.apache.struts2.dispatcher.ActionContextCleanUp
        2、其他listener,如:SiteMesh,apache titles,CharacterEncodingFilter,UrlRewriteFilter......
        3、org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

5、完整的web.xml配置内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            WEB-INF/applicationContext.xml
        </param-value>
    </context-param>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <servlet>
        <servlet-name>LoginGame</servlet-name>
        <servlet-class>com.xz.LoginValidationServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginGame</servlet-name>
        <url-pattern>/loginGame</url-pattern>
    </servlet-mapping>
    
    <filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
    </filter>
    <filter>
        <filter-name>encodingFilter</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>
        <filter-name>UrlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
        <init-param>
          <param-name>logLevel</param-name>
          <param-value>WARN</param-value>
        </init-param>
  </filter>
    <filter>
        <filter-name>UrlFilter</filter-name>
        <filter-class>com.xz.UrlFilter</filter-class>
    </filter>
    <filter>
        <filter-name>GameFilter</filter-name>
        <filter-class>com.xz.GameFilter</filter-class>
    </filter>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    
    <!-- mappings -->
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>*.html</url-pattern>
  </filter-mapping>
    <filter-mapping>
        <filter-name>UrlFilter</filter-name>
        <url-pattern>/member/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>GameFilter</filter-name>
        <url-pattern>/game/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>  
          <dispatcher>FORWARD</dispatcher>  
          <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>

    <!-- contextConfigLocation param loaded class -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>com.xz.listener.OfficeListener</listener-class>
    </listener>

    <!-- welcome list -->
    <servlet>
        <servlet-name>dwr-invoker</servlet-name>
        <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>false</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dwr-invoker</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>
  <servlet-mapping>
    <servlet-name>DepositsServlet</servlet-name>
    <url-pattern>/deposits</url-pattern>
  </servlet-mapping>


    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- exception pages -->
    <error-page>
        <error-code>404</error-code>
        <location>/404.jsp</location>
    </error-page>
    <error-page>
        <error-code>403</error-code>
        <location>/403.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/500.jsp</location>
    </error-page>
    <error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/500.jsp</location>
    </error-page>

</web-app>

6、urlrewrite.xml的编写示例:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
        "http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
<!--
    Configuration file for UrlRewriteFilter

http://tuckey.org/urlrewrite/

-->
<urlrewrite decode-using="utf-8">
    <rule>
        <from>^/message-u-(.*)\.html$</from>
        <to>/testAction.do?username=$1</to>
    </rule>
    <rule>
        <from>^(.*)/a-([0-9]+)\.html$</from>
        <to>/index.jsp?fid=$2</to>
    </rule>
    <rule>
        <from>^/b-(username|fid)-([0-9]+)\.html</from>
        <to>/index2.jsp?$1=$2</to>
    </rule>
    <rule>
        <from>^(.*)/forum-([0-9]+)-([0-9]+)\.html</from>
        <to>/forumdisplay.jsp?fid=$2&page=$3</to>
    </rule>
    <rule>
        <from>^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html</from>
        <to>/viewthread.jsp?tid=$2&extra=page\%3D$3&page=$4</to>
    </rule>
    <rule>
        <from>^(.*)/tag-(.+)\.html</from>
        <to>/tag.jsp?name=$2</to>
    </rule>
    <rule>
        <from>^(.*)/space-(username|uid)-(.+)\.html$</from>
        <to>/space.jsp?$2=$3</to>
    </rule>
    <rule>
        <from>^/archiver/fid-([0-9]+)\.html$</from>
        <to>/archiver/index.jsp?fid=$1</to>
    </rule>
    <outbound-rule>
        <note>
            The outbound-rule specifies that when response.encodeURL is
            called (if you are using JSTL c:url) the url /rewrite-status
            will be rewritten to /test/status/. The above rule and this
            outbound-rule means that end users should never see the url
            /rewrite-status only /test/status/ both in thier location
            bar and in hyperlinks in your pages.
        </note>
        <from>/rewrite-status</from>
        <to>/test/status/</to>
    </outbound-rule>
</urlrewrite>

抱歉!评论已关闭.