How to call custome URL action from Form action?

柔情痞子 提交于 2019-11-29 12:54:37

First of all you should get rid of action extension, if you don't want user to think their name has an extension.

<constant name="struts.action.extension" value=",,action"/> 

Next the pattern matcher should be regex.

<constant name="struts.patternMatcher" value="regex"/>

Action mapping

<action name="/CustomURL/{username}" class="com.rajesh.struts2.CustomURL" method="customUrl">
    <result name="success">welcome.jsp</result>
</action>

In the JSP you don't need to use form tag, but anchor tag. And use known names.

<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;
 }

Try $ before {username}. $ Stands for Freemarker

<action name="{username}" class="com.rajesh.struts2.CustomURL" 
 method="customUrl">

Freemarker Tutorial

How to use OGNL Expression Example link

<s:property value="username"/> for calling username on welcome page.

It may help you to get your answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!