http://localhost:8080/CustomURL%7Busername%7D.action;jsessionid=9C1FB3EB633209C18625BBB40EA61000
我只想喜欢 http://localhost:8080/CustomURL/rajesh
http://localhost:8080/CustomURL/rajesh
看到我的Struts.xml
<struts> <constant name="struts.mapper.alwaysSelectFullNamespace" value="false" /> <constant name="struts.enable.SlashesInActionNames" value="true" /> <constant name="struts.patternMatcher" value="namedVariable" /> <package name="default" namespace="/" extends="struts-default"> <action name=""> <result name="success">home.jsp</result> </action> <action name="{username}" class="com.rajesh.struts2.CustomURL" method="customUrl"> <result name="success">welcome.jsp</result> </action> </package>
看我的jsp页面
<%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Struts 2 Custom URL</title> </head> <body> <h1>Struts 2 Custom URL</h1> <h3>Enter your name below</h3> <s:form action="{username}"> <s:textfield name="username" /> <s:submit /> </s:form> </body> </html>
请参阅下面的Java文件。
public class CustomURL extends ActionSupport { private String username; public String getUsername() { System.out.println("Getter"); return username; } public void setUsername(String username) { System.out.println("Setter"); this.username = username; } private static final long serialVersionUID = -4337790298641431230L; public String customUrl() { return SUCCESS; } }
请提出任何建议。
首先,如果您不希望用户认为他们的名字带有扩展名,则应该删除操作扩展名。
<constant name="struts.action.extension" value=",,action"/>
接下来,模式匹配器应为regex。
regex
<constant name="struts.patternMatcher" value="regex"/>
动作映射
<action name="/CustomURL/{username}" class="com.rajesh.struts2.CustomURL" method="customUrl"> <result name="success">welcome.jsp</result> </action>
在JSP中,您不需要使用form标记,而是使用锚标记。并使用已知的名称。
form
<a href="http://localhost:8080/CustomURL/rajesh">Click my name</a>