我按照这篇文章创建了一个自定义URL应用程序。操作正在被调用,但url显示会话id为
http://localhost:8080/CustomURL%7Busername%7D.action;jsessionid=9C1FB3EB633209C18625BBB40EA61000
我想简单的像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;
}
}
如果你不想让用户认为他们的名字有扩展名,首先你应该去掉action扩展名。
<constant name="struts.action.extension" value=",,action"/>
接下来模式匹配器应该是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
标记,而需要使用锚标记。并使用已知的名字。
<a href="http://localhost:8080/CustomURL/rajesh">Click my name</a>
try it.
public String customUrl() {
HttpServletRequest request=(HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
String url="http://"+request.getServerName()+":"+request.getServerPort()+""+request.getContextPath()+"/"+request.getParameter("username");
logger.info(" Current url : "+url); //check it here
return SUCCESS;
}
在{username}之前尝试$
。$
表示Freemarker
<action name="{username}" class="com.rajesh.struts2.CustomURL"
method="customUrl">
Freemarker教程
如何使用OGNL表达式示例link
<s:property value="username"/> for calling username on welcome page.
它可能会帮助你得到答案。