How to call custome URL action from Form action?

不问归期 提交于 2019-11-28 07:04:10

问题


I followed this post and created a custom URL application. The action is getting called but the url shows with session id like

http://localhost:8080/CustomURL%7Busername%7D.action;jsessionid=9C1FB3EB633209C18625BBB40EA61000

I want simply like http://localhost:8080/CustomURL/rajesh

See my 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>

See my jsp page

<%@ 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>

See java file below.

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

Any suggestion please.


回答1:


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>



回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/23819733/how-to-call-custome-url-action-from-form-action

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