嗨,我是初學者到struts2。如果我像後 https://stackoverflow.com/questions/25658104
的URL我想獲得價值25658104
在Struts2中清潔url-s
它必須不帶任何參數來完成。還不如
https://stackoverflow.com/questions?qid=25658104
有什麼辦法基於之間傳遞的價值來獲得內容/ myvalue的/
如果我發佈https://stackoverflow.com/questions/myvalue1/
- >重定向到一個頁面,對應myvalue1(從DB)的負載值
如果我發佈https://stackoverflow.com/questions/myvalue2/
- >重定向到一個頁面,對應於(從DB)
我發現它在myvalue2負荷值,因此基於之間傳遞的價值觀網站導航所以導航。如何我可以使用它在S truts2
我的問題是類似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;
}
}
你真的想** **重寫URL-S還是你只是想要搜索引擎優化友好的網址? – 2014-09-04 07:22:01
如果你只需要乾淨的網址,看看:http://stackoverflow.com/q/19495332/1654265和http://stackoverflow.com/a/22068583/1654265 – 2014-09-04 08:16:13
謝謝@AndreaLigios你的第二個鏈接恰好給我看我引用了http://www.struts2.info/blog/better-urls-with-struts2,但它沒有設置變量的值。action class中的值爲** null ** – theRoot 2014-09-04 09:22:23