如何在 Struts 2 中编写漂亮的 URL? [英] How to write a pretty URL in Struts 2?

查看:26
本文介绍了如何在 Struts 2 中编写漂亮的 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Struts2 中重写我的 URL.

I want to rewrite my URL in Struts2.

如何自定义不应显示我正在传递的参数并显示不同名称的操作的 URL.

How can I customize URL which shouldn't show my parameter that I am passing and also show action with different name.

我不知道该怎么做.我必须为此使用插件或在 struts.xml 中配置我的代码:

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>

告诉我如何执行以及给我解决方案.

Tell me the way how to perform as well as give me solution.

我的网址是这样的:

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

我想显示我的网址

/operations/prediction 

对用户来说,我想根据其目的对所有 URL 做同样的事情.

to the user and the same thing I want to do for all URLs based on its purpose.

推荐答案

如果你不想显示参数传递,你可以使用 这个解决方案.

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

要隐藏传递的参数实际上你需要提交表单.您应该阻止 click 事件的默认行为并将其替换为 form 事件.

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.

从 2.1.9+ 开始可以在动作中定义正则表达式名称.要使用这种形式的通配符,以下常量必须是设置:

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" />

正则表达式可以有两种形式,最简单的一种是{FIELD_NAME},在这种情况下,字段中包含 FIELD_NAME操作将填充匹配的文本,例如:

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>

这篇关于如何在 Struts 2 中编写漂亮的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆