-1

I am expecting my code to reformat the URL and show them as following (with / )

 http://www.example.com/myProject/Profile/view.action
 http://www.example.com/myProject/Profile/edit.action

but it shows them as following (with _ )

http://www.example.com/myProject/Profile_view.action
http://www.example.com/myProject/Profile_edit.action

To make that, I changed the " _ " to "/" in my struts.xml file but it does not work

<action name="Profile/*" method="{1}" class="com.controller.Profile">
      <result name="view" tiles="viewProfile">viewProfile</result>
       <result name="edit" tiles="editProfile">editProfile</result>
  </action>

I am calling that using following code

 <a href="Profile/view.action" >Profile</a>

Please let me know if there is any other method to implement it.

Daniel Morgan
  • 782
  • 5
  • 15
  • 43
  • 1
    After changing the _ to / did you restart the web application? Anyways you shouldn't be able to use slashes in action names unless you set struts.enable.SlashesInActionNames to true. You did this? – Quaternion Feb 14 '13 at 06:19
  • I don't understand what is it a correct URL, Is the URL format of the addresses is incorrect or do you need a specification with to formatting URLs? – Roman C Feb 14 '13 at 11:32
  • @RomanC, I changed the word to rewrite. – Daniel Morgan Feb 14 '13 at 22:21

3 Answers3

1

why are you calling your action like

<a href="Profile/view.action" >Profile</a>

however you can achieve this using

<a href="view.action" >Profile</a>

and made changes in your xml

<action name="view.action" method="{1}" class="com.controller.Profile">
      <result name="view" tiles="viewProfile">viewProfile</result>
       <result name="edit" tiles="editProfile">editProfile</result>
</action>

you are not allowed to use wildcards here in your mapping for method="{1}",because in your action you have not specified anything.And if you really want to use wildcards then specify a method name in your action like this

<a href="YOURMETHODNAMEview.action" >Profile</a>

and in your xml

 <action name="*view.action" method="{1}" class="com.controller.Profile">
          <result name="view" tiles="viewProfile">viewProfile</result>
           <result name="edit" tiles="editProfile">editProfile</result>
    </action>
arvin_codeHunk
  • 2,328
  • 8
  • 32
  • 47
1

If your really want to separate your actions with slash, you should use NAMESPACE, try this:

<package name="profile" extends="struts-default" namespace="/Profile">
    <action name="*" method="{1}" class="com.controller.Profile">
      <result name="view" tiles="viewProfile">viewProfile</result>
      <result name="edit" tiles="editProfile">editProfile</result>
    </action>
</package>

And if you are trying to use parameters on the url, you should consider use

<constant name="struts.enable.SlashesInActionNames" value="true"/>

on your Struts 2 configuration file.

Regards to the Wildcart Mapping document, you can also do this:

    <action name="**" method="{1}" class="com.controller.Profile">
      <result name="view" tiles="viewProfile">viewProfile</result>
      <result name="edit" tiles="editProfile">editProfile</result>
    </action>

** matches zero or more characters including the slash ('/') character. You can also find this on the Wildcart Mapping document.

You should think about what you really want first, then do the configuration and implementation.

In your case, Struts 2 thinks that you have slash on your action, regards to the Action Configuration

Action Names With Slashes
If your action names have slashes in them (for example, <action name="admin/home" class="tutorial.Admin"/>) you need to specifically allow slashes in your action names via a constant in the struts.xml file by specifying <constant> name="struts.enable.SlashesInActionNames" value="true"/>.
See JIRA Issue WW-1383 for discussion as there are side effects to setting this property to true.

Action Names with Dots and Dashes
Although action naming is pretty flexible, one should pay attention when using dots (eg. create.user) and/or dashes (eg. my-action). While the dot notation has no known side effects at this time, the dash notation will cause problems with the generated JavaScript for certain tags and themes. Use with caution, and always try to use camelcase action names (eg. createUser) or underscores (eg. my_action).

So Struts 2 will convert your slash to an underscore.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Jaiwo99
  • 9,687
  • 3
  • 36
  • 53
  • thanks it works but with a minor problem as following http://stackoverflow.com/questions/14886855/how-to-change-my-code-to-implement-url-reqriting-correctly – Daniel Morgan Feb 15 '13 at 01:05
0

By default, struts does not allow / in action name. You can achieve what you want by using namespace or modify the struts config to allow dashes in action.

1.To use namespace, define another package with namespace "/Profile":

<package name="profile" extends="struts-default" namespace="/Profile">
<action name="*" method="{1}" class="com.controller.Profile">
  <result name="view" tiles="viewProfile">viewProfile</result>
  <result name="edit" tiles="editProfile">editProfile</result>
</action>
......
</package>

This method is good if you want to group many actions under the same namespace.

2.To allow dashes in action, you have 2 options, to define constants in struts.xml like below:

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

or to define it in struts.properties:

struts.enable.SlashesInActionNames=true
struts.mapper.alwaysSelectFullNamespace=false

After that you can use this in struts.xml:

<action name="Profile/*" method="{1}" class="com.controller.Profile">
  <result name="view" tiles="viewProfile">viewProfile</result>
   <result name="edit" tiles="editProfile">editProfile</result>
</action>
Tony Vu
  • 4,251
  • 3
  • 31
  • 38