Functions and configuration example of urlrewritefilter

Source: Internet
Author: User

Currently, the Q & A system is mainly implemented in the form of Baidu Q & A. It is also used in the combination of campus characteristic modules to enhance the internal Q & A style on campus. The new feature uses urlrewritefilter technology.

I. Role: static dynamic URLs facilitate search engine crawlers to capture your dynamic web pages. Hiding Technology to Improve the portability of websites.

Ii. configuration method and demo

Operating System: Win 7 32-bit flagship Edition

Software environment: myeclipse 9.0 + Tomcat 7.x

 

Step 1: Import urlrewrite-2.6.0.jar, configure web. xml

<! -- Configure urlrewritefilter --> <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-mapping> <filter-Name> urlrewritefilter </filter-Name> <URL-pattern>/* </url-pattern> </filter- mapping> <filter-Name> struts2 </filter-Name> <filter-class> Org. apache. struts2.dispatcher. filterdispatcher </filter-class> </filter> <filter-mapping> <filter-Name> struts2 </filter-Name> <URL-pattern> *. htm </url-pattern> <dispatcher> request </dispatcher> <dispatcher> forward </dispatcher> <dispatcher> include </dispatcher> </filter-mapping> <filter- mapping> <filter-Name> struts2 </filter-Name> <URL-pattern> *. jhtm </url-pattern> <dispatcher> request </dispatcher> <dispatcher> forward </dispatcher> <dispatcher> include </dispatcher> </filter-mapping>

Step 2: Create urlrewrite. xml under the WEB-INF folder

 

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype urlrewrite public "-// tuckey.org//dtd urlrewrite 2.6 // en" http://tuckey.org/res/dtds/urlrewrite2.6.dtd "> <urlrewrite> <rule> <Note> Home </Note> <from> ^/index.html $ </from> <to>/user/userlist </to> </Rule> <rule> <Note> homepage </Note> <from> ^/$ </from> <to>/user/userlist </to> </Rule> <rule> <Note> administrator background logon </Note> <from> ^/admin/login.htm $ </from> <to>/admin/login/tologin.htm </to> </Rule> <rule> <Note> administrator background </Note> <from> ^/admin (\/*) $ </from> <to>/admin/login/tologin.htm </to> </Rule> </urlrewrite>

Step 3: Add the following code to struts. xml:

<package name="login" namespace="/admin/login" extends="json"><action name="toLogin" class="indexAction" method="toLogin"><result name="success">/WEB-INF/page/admin/login/login.jsp</result></action><action name="doLogin" class="indexAction" method="doLogin"></action></package>

 

Step 4: Write the corresponding action:

/*** Background login ** @ author 正5-5-5 10:04:51 */@ controller @ scope ("prototype") public class indexaction extends baseaction {Private Static final long serialversionuid = 1l; @ autowiredprivate iloginservice loginservice; @ autowiredprivate ipermissionservice perpermissionservice;/*** jump to the logon page ** @ return */Public String tologin () {return success ;} /*** jump to the background homepage ** @ return */Public String index () {getrequest (). setattribute ("adminame", cookieutil. getcookievalue (constant. admin_name, getrequest (); Return success;}/*** exit the system ** @ return */}

Note: When you enter the access URL: http: // localhost: 8087/iask/admin, the suffix ". JSP "does not exist. This address is accessed through urlrewrite. XML filter is parsed to http: // localhost: 8087/iask/admin/login/tologin.htm. the XML file parses the namespace as/admin/login/, and then finds the corresponding action for processing.

 

After processing: the access URL is http: // localhost: 8087/iask/admin.

Step 5: Test

 

URL entered in the test: http: // localhost: 8087/iask/admin1

Http: // localhost: 8087/iask/admintest

Http: // localhost: 8087/iask/admin454 all show Error 404

 

Input: http: // localhost: 8087/iask/admin. The test passes.

 

Test: http: // localhost: 8087/iask/admin/index.htm? R = 0.49279468820504096 test successful

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.