您好,我是 struts2 的初学者。如果我发布一个 URL
https://stackoverflow.com/questions/25658104
我想获取值 25658104
It must be done without any parameter. not as
https://stackoverflow.com/questions?qid=25658104
有没有办法根据/myvalue/之间传递的值获取内容
如果我发布 https://stackoverflow.com/questions/myvalue1/
-->重定向到一个页面并加载对应于 myvalue1 的值(来自数据库)
如果我发布 https://stackoverflow.com/questions/myvalue2/
-->重定向到一个页面并加载对应于 myvalue2 的值(来自数据库)
我根据它导航的 SO 之间传递的值在 SO 站点导航中找到它。我如何在 Struts2 中使用它
我的问题类似于URL rewriting with PHP 但我想在 struts2 中做到这一点
编辑
struts.xml
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<constant name="struts.multipart.maxSize" value="104857600" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.patternMatcher" value="username"/>
<bean type="com.opensymphony.xwork2.util.PatternMatcher" name="username" class="com.opensymphony.xwork2.util.NamedVariablePatternMatcher"/>
<package name="default" extends="struts-default" namespace="/">
<result-types>
<result-type name="json" default="false" class="org.test.struts.controller.JSONResult" />
</result-types>
<action name="login" class="org.test.struts.controller.LoginAction" method="execute">
<result name="success">Welcome.jsp</result>
<result name="error">Login.jsp</result>
</action>
<action name="profiles/{username}" class="org.test.struts.controller.ViewProfileAction">
<result name="input">profile.jsp</result>
</action>
<action name="getJson" class="org.test.struts.controller.JsonTestAction">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
<action name="getJsonObj" class="org.test.struts.controller.JsonTest">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
<action name="getJsonObject" class="org.test.struts.controller.JsonObjectRet">
<result name="success" type="json">
<param name="target">startswith</param>
<param name="targetObjectClass">java.lang.String</param>
</result>
</action>
<action name="upload">
<result>/UploadFile.jsp</result>
</action>
<action name="UploadFile" class="org.test.struts.fileupload.controller.UploadFileAction">
<param name="filesPath">myfiles</param>
<result name="success">/UploadFileSuccess.jsp</result>
<result name="input">/UploadFile.jsp</result>
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">10485760</param>
<param name="fileUpload.allowedTypes">text/plain,image/jpeg</param>
</interceptor-ref>
</action>
</package>
</struts>
Action 类
import com.opensymphony.xwork2.ActionSupport;
public class ViewProfileAction extends ActionSupport{
private String username;
public String getUsername() {
return username;
}
public String execute() {
// look up the appropriate user by username and
// expose the user to the JSP with a getUser() method.
System.out.println(username);
return INPUT;
}
public void setUsername(String username) {
this.username = username;
}
}
最佳答案
改变这个
<constant name="struts.patternMatcher" value="username"/>
<bean type="com.opensymphony.xwork2.util.PatternMatcher" name="username" class="com.opensymphony.xwork2.util.NamedVariablePatternMatcher"/>
为此,使用正则表达式 patternMatcher:
<constant name="struts.patternMatcher" value="regex"/>
当然,除了 input
之外,还要为您的 Action 添加一个结果!!喜欢
<action name="profiles/{username}" class="org.test.struts.controller.ViewProfileAction">
<result name="success">profile.jsp</result>
<result name="input">profile.jsp</result>
</action>
并从 Action 中返回 SUCCESS
而不是 INPUT
。
INPUT
是 Workflow Interceptor 在发生输入错误时返回的自动结果,甚至没有达到 Action(阅读 detailed answer 关于它)
关于java - 清理 Struts2 中的 url-s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25658841/