0

I have created form login.jsp and when user enter username & password then i want to create URL like : /companyName/username.

Example : If company name is abc and username is parth1 then URL is

"http://localhost:8080/Temp_Central_Emp/abc/parth1/afterLogin.jsp"

I get companyName from the database based on the username.

File : login.jsp:

<s:form action="afterLogin" method="post">
    <s:textfield label="Enter username" key="userId" maxlength="5" size="30" id="userId"/>
    <s:password label="Enter password" key="userPsw" size="30" />
    <tr>
        <td></td>
        <td>
            <img src="<c:url value='simple-captcha.png' />" />
            <br />
            <p>Press F5 for refresh.</p>
        </td>
    </tr>
    <s:textfield label="Enter code" key="captchaResponse" size="30" />
    <s:submit value="Login" />
</s:form>

File : struts.xml:

<struts>
<constant name="struts.enable.SlashesInActionNames" value="true"/> 
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.devMode" value="true" />
<package name="a" extends="struts-default">
 
    <action name="afterLogin" class="com.parth.LoginAction">
        <result name="success">/afterLogin.jsp</result>
    </action>

</package>

File : LoginAction.java:

public class LoginAction extends ActionSupport{

private Map<String, Object> session;
private String userId;
private String userPsw;
private String url;

private static final long serialVersionUID = 1L;

@Override
@Action(value="{url}/afterLogin")
public String execute() throws Exception {      
    // business logic to insert user into database
    url = "/companyName/" + userId;
    return SUCCESS;
}

 /* Getters and Setters */
}

I refer this link but my need is different. Creating dynamic URL with 2 action parameters in Struts 2 using OGNL

I also try code from this link but still i'm not solving it.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Parth Patel
  • 799
  • 1
  • 13
  • 20
  • check this out http://stackoverflow.com/questions/23780282/how-to-create-custom-urls-with-struts2like-www-twitter-com-goodyzain – goodyzain Feb 21 '17 at 09:20
  • @goodyzain when i add **** i receive error. And i also changed my code based on the answer on your question but stil not work. If you know how to generate dynamic url then please provide me code based on the above question. – Parth Patel Feb 21 '17 at 09:32
  • whats the error you are getting when you add Advanced wildCard ?? – goodyzain Feb 21 '17 at 09:39
  • In browser : **HTTP Status 404 - /Temp_Central_Emp/** In console : "java.lang.RuntimeException: Unable to load bean com.opensymphony.xwork2.util.PatternMatcher (regex) - [unknown location]" – Parth Patel Feb 21 '17 at 09:42
  • @goodyzain In your application what you wrote action name in form? – Parth Patel Feb 21 '17 at 09:53
  • @goodyzain I successfully transfer the page but in url i receive **/%7Burl%7D** instead of **/companyName/parth**. – Parth Patel Feb 21 '17 at 10:25
  • 1
    @ParthPatel you should post the stacktrace – Roman C Feb 21 '17 at 13:15
  • @RomanC can you explain what is stacktrace? I'm new on struts2. – Parth Patel Feb 21 '17 at 13:17
  • @ParthPatel It's what is printed in the console when exception is thrown. – Roman C Feb 21 '17 at 14:14

2 Answers2

0

try to put that url variable in your struts.xml:

<action name="afterLogin" class="com.parth.LoginAction">
    <result name="success">${url}/afterLogin.jsp</result>
</action>

and remove your annotation Action here:

@Override
//@Action(value="{url}/afterLogin") // remove this
public String execute() throws Exception {      
    // business logic to insert user into database
    url = "/companyName/" + userId;
    return SUCCESS;
}
msagala25
  • 1,806
  • 2
  • 17
  • 24
0

The result should be of redirect action type if you want to change the URL and follow PRG pattern. See Parameters in configuration results:

<result name="success" type="redirectAction">afterLogin?companyName=${companyName}&userId=${userId}</result>

<action name="afterLogin">
   <result>/afterLogin.jsp</result>
</action>
Roman C
  • 49,761
  • 33
  • 66
  • 176