如何从表单操作调用自定义URL操作?

时间:2014-05-23 01:37:39

标签: java jsp struts2 wildcard-mapping

我关注this post并创建了自定义网址应用程序。该操作正在被调用,但网址会显示会话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;
    }
}

请提出任何建议。

3 个答案:

答案 0 :(得分:0)

在{username}之前尝试$$代表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.

它可以帮助您得到答案。

答案 1 :(得分:0)

试试..

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

答案 2 :(得分:0)

首先,如果您不希望用户认为他们的名字有扩展名,您应该摆脱行动扩展。

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