3

Hi i am beginner to struts2. If i post an URL like https://stackoverflow.com/questions/25658104 I want to get value 25658104

It must be done without any parameter. not as https://stackoverflow.com/questions?qid=25658104

Is there any way to get content based on the value passed between /myvalue/

if I post https://stackoverflow.com/questions/myvalue1/-->redirects to a page and loads values corresponding to myvalue1 (from DB)

if I post https://stackoverflow.com/questions/myvalue2/-->redirects to a page and loads values corresponding to myvalue2(from DB)

I found it in SO site navigation based on the values passed between SO it navigates .How can i use it in Struts2

My question is similar to URL rewriting with PHP but i want to do it in struts2

EDIT

struts.xml

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<constant name="struts.multipart.maxSize" value="104857600" />


<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.patternMatcher" value="username"/>

<bean type="com.opensymphony.xwork2.util.PatternMatcher" name="username" class="com.opensymphony.xwork2.util.NamedVariablePatternMatcher"/>

<package name="default" extends="struts-default" namespace="/">

    <result-types>
        <result-type name="json" default="false" class="org.test.struts.controller.JSONResult" />
    </result-types>

    <action name="login" class="org.test.struts.controller.LoginAction"  method="execute">
        <result name="success">Welcome.jsp</result>
        <result name="error">Login.jsp</result>
    </action>

    <action name="profiles/{username}" class="org.test.struts.controller.ViewProfileAction">
        <result name="input">profile.jsp</result>
    </action>




    <action name="getJson" class="org.test.struts.controller.JsonTestAction">
      <result type="stream">
        <param name="contentType">text/html</param>
        <param name="inputName">inputStream</param>
     </result>
   </action>

   <action name="getJsonObj" class="org.test.struts.controller.JsonTest">
      <result type="stream">
        <param name="contentType">text/html</param>
        <param name="inputName">inputStream</param>
     </result>
   </action>

   <action name="getJsonObject" class="org.test.struts.controller.JsonObjectRet">
      <result name="success" type="json">
                 <param name="target">startswith</param>
                 <param name="targetObjectClass">java.lang.String</param>
      </result>
   </action>


    <action name="upload">
        <result>/UploadFile.jsp</result>
    </action>
    <action name="UploadFile" class="org.test.struts.fileupload.controller.UploadFileAction">
        <param name="filesPath">myfiles</param>
        <result name="success">/UploadFileSuccess.jsp</result>
        <result name="input">/UploadFile.jsp</result>

        <interceptor-ref name="defaultStack">
            <param name="fileUpload.maximumSize">10485760</param>
            <param name="fileUpload.allowedTypes">text/plain,image/jpeg</param>
        </interceptor-ref>

    </action>


</package>
</struts>

Action class

import com.opensymphony.xwork2.ActionSupport;

public class ViewProfileAction extends ActionSupport{
  private String username;

  public String getUsername() {
    return username;
}


public String execute() {
    // look up the appropriate user by username and
    // expose the user to the JSP with a getUser() method.
      System.out.println(username);
      return INPUT;
  }


  public void setUsername(String username) {
    this.username = username;
  }
}
Community
  • 1
  • 1
theRoot
  • 571
  • 10
  • 33
  • Do you want actually **rewrite** url-s or do you just want SEO-friendly url-s? – Aleksandr M Sep 04 '14 at 07:22
  • 1
    If you only need clean urls, take a look at: http://stackoverflow.com/q/19495332/1654265 and http://stackoverflow.com/a/22068583/1654265 – Andrea Ligios Sep 04 '14 at 08:16
  • thanks @AndreaLigios ur second link exactly showed me the way .I referred http://www.struts2.info/blog/better-urls-with-struts2 but it does not set the value for the variable in action.It values in action class is **null** – theRoot Sep 04 '14 at 09:22
  • @name_not_found_Exception glad that helped. Remember to use `","` instead of `""` for action extension, as pointed out by AleksandrM in comments, and consider upvoting that answer if it helped – Andrea Ligios Sep 04 '14 at 09:23
  • @AleksandrM SEO-friendly url-s – theRoot Sep 04 '14 at 09:26
  • @AndreaLigios i followd as per the blog but my action class variables are not set.It has null value. – theRoot Sep 04 '14 at 09:34
  • @name_not_found_Exception The same things explained in Steven's blog are explained in the doc reported in my first link. I used regex patternMatcher instead of namedVariable, though, that I prefer. Have you correctly set the three attributes in struts.xml ? Are you using Convention ? You should edit your question with the code you used, so we can figure out where the problem is – Andrea Ligios Sep 04 '14 at 09:39
  • @AndreaLigios i have posted my code – theRoot Sep 04 '14 at 09:47

1 Answers1

1

Change this

<constant name="struts.patternMatcher" value="username"/>

<bean type="com.opensymphony.xwork2.util.PatternMatcher" name="username" class="com.opensymphony.xwork2.util.NamedVariablePatternMatcher"/>

to this, using the regex patternMatcher:

<constant name="struts.patternMatcher" value="regex"/>

And of course add a result for your Action other than the input one !! Like

<action name="profiles/{username}" class="org.test.struts.controller.ViewProfileAction">
    <result name="success">profile.jsp</result>
    <result name="input">profile.jsp</result>
</action>

And return SUCCESS from Action instead of INPUT.

INPUT is an automated result returned by Workflow Interceptor when input errors occour, and the Action is not even reached (read a detailed answer about it)

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • it is showing me error `Unable to load bean com.opensymphony.xwork2.util.PatternMatcher (regex) - [unknown location]` .........Becaus of this i included PatternMatcher bean – theRoot Sep 04 '14 at 09:57
  • You do need Struts version higher than 2.1.9 – Andrea Ligios Sep 04 '14 at 09:58
  • 1
    If you're using a lower version, I strongly suggest you to upgrade immediately to 2.3.16 or higher, due to dangerous security issues afflicting old versions – Andrea Ligios Sep 04 '14 at 09:59
  • okie thanks for ur help i wil try with higher version...currently i'm using 2.1.6 :( – theRoot Sep 04 '14 at 10:09
  • commons-logging-1.1.3.jar freemarker-2.3.19.jar ognl-3.0.6.jar struts2-core-2.3.16.3.jar xwork-core-2.3.16.3.jar does these jars are enough for running the project – theRoot Sep 04 '14 at 10:39
  • I tried Advanced Wildcards as mentioned int he official docs, but it isn't working. https://struts.apache.org/core-developers/wildcard-mappings.html#advanced-wildcards I am using `Struts 2.5.29` Any idea, why it isn't working? – Kavin Raju S Feb 08 '22 at 17:02