How to call custome URL action from Form action?
Asked Answered
P

3

0

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;
    }
}
Physicochemical answered 23/5, 2014 at 1:37 Comment(0)
H
0

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>
Hegelian answered 23/5, 2014 at 6:7 Comment(2)
@Roman C sir i have a doubt. when i use regex it is not working. when i use namedVariable it is working. why it is happening?Dishonorable
@Dishonorable regex pattern matcher might be not available in the struts version you use.Hegelian
D
0

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.

Dishonorable answered 23/5, 2014 at 4:37 Comment(4)
and if still it won't work then please post your action file means your java file in your question.Dishonorable
It's not working. I added the java file in the questionPhysicochemical
<constant name="struts.patternMatcher" value="regex"/> try thisDishonorable
I tried it is not working with <s:form> tag. because it is not taking dynamically username from textbox. and forward. without form tag in url your domain/anyname it will work you will get success page. @RajeshDishonorable
B
0

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;
 }
Bearish answered 23/5, 2014 at 5:42 Comment(0)
H
0

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>
Hegelian answered 23/5, 2014 at 6:7 Comment(2)
@Roman C sir i have a doubt. when i use regex it is not working. when i use namedVariable it is working. why it is happening?Dishonorable
@Dishonorable regex pattern matcher might be not available in the struts version you use.Hegelian

© 2022 - 2024 — McMap. All rights reserved.