Как вызвать обычное действие URL из формы действий?

Я последовал за этим постом и создал пользовательское приложение URL. Действие вызывается, но URL отображается с идентификатором сессии, как

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 ответа

Решение

Прежде всего вы должны избавиться от расширения действия, если вы не хотите, чтобы пользователь думал, что его имя имеет расширение.

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

Попытайся..

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

Пытаться $ до {имя пользователя}. $ Стенды для Freemarker

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

Учебник по Freemarker

Как использовать ссылку на пример выражения OGNL

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

Это может помочь вам получить ваш ответ.

Другие вопросы по тегам