如何从表单操作调用自定义URL操作?
我按照这篇文章创建了一个自定义URL应用程序。 该动作正在被调用,但是url显示的是会话ID
http://localhost:8080/CustomURL%7Busername%7D.action;jsessionid=9C1FB3EB633209C18625BBB40EA61000
我想简单地喜欢http://localhost:8080/CustomURL/rajesh
请参阅我的Struts.xml
home.jsp welcome.jsp
看我的jsp页面
Struts 2 Custom URL Struts 2 Custom URL
Enter your name below
请参阅下面的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; } }
有任何建议请。
首先,如果您不希望用户认为他们的名字有扩展名,您应该摆脱动作扩展。
接下来模式匹配器应该是regex
。
动作映射
welcome.jsp
在JSP中,您不需要使用form
标记,而是使用锚标记。 并使用已知名称。
Click my name
试试吧..
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
Freemarker教程
如何使用OGNL表达示例链接
for calling username on welcome page.
它可以帮助您得到答案。