How to wtite pretty URL in Struts2.x?

2020-03-31 07:04发布

问题:

I want to rewrite my URL in Struts2. Please help me out to do this. How can i customize as i want, i don't want to show my parameter that i am passing and also show action with different name that i want I don't know how to do it. i have to use plugins for that or configure my code in struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.action.extension" value="action" />
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.ui.theme" value="simple" />  


    <include file="struts-user.xml"></include>
    <include file="struts-admin.xml"></include>
    <include file="struts-common.xml"></include>
    <include file="struts-masters.xml"></include>
</struts>

file tell me the way how to perform as well as give me solution.

my URL is like this

/operations/MatchPredictionLevel2_index.action?sgt=102&lvl2=1&tn=MatchPredictionLevel2

i want to show my url like /operations/prediction to user and the same thing i want to do for all URL based on its purpose i want rewrite url

回答1:

If you don't want to show the parameter passing you could use this solution.

To hide passed parameter actually you need to submit the form. You should prevent the default behavior of the click event and replace it with the form event.

If you also want to show action with different name, you can use the above filter or wildcard mapping.

Advanced Wildcards

From 2.1.9+ regular expressions can be defined defined in the action name. To use this form of wild card, the following constants must be set:

<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.patternMatcher" value="regex" />

The regular expressions can be in two forms, the simplest one is {FIELD_NAME}, in which case the field with the FIELD_NAME in the action will be populated with the matched text, for example:

<package name="books" extends="struts-default" namespace="/">
    <action name="/{type}/content/{title}" class="example.BookAction">
  <result>/books/content.jsp</result>
    </action>
</package>