开发者

How to parse Request URL in Struts 2?

The registered accounts in my web application (Created using s开发者_高级运维truts2) gets a separate site like http://localhost/accountname

And each account has its own login page. After registering, the users will get a separate site http://localhost/accountname

I want to scan the request URL (in struts2) first and then forward that request to the corresponding login page.

how can i do this? please help


You can get the request object in your action method by:

   HttpServletRequest request = ServletActionContext.getRequest();

You can then find the request URL like this:

   String spath = request.getServletPath();

Then you can parse it and look for the pattern that you want and forward accordingly.


UPDATE:

You can use a package in the struts.xml configuration file. Lets say "userapps".

     <package name="userapps" extends="default" namespace="/userapps">
           <action name="*" class="path.to.your.ActionClass" method="processUrl">
               <result name="success" type="redirectAction">
                   <param name="actionName">userpage</param>
                   <param name="id">${user.id}</param>
               </result>
           </action>
     </package>

In the ActionClass's processURL method you can pull out the part of the URL that you are interested in and set a property lets say the user and his id. You then return success from your action.

You will have a second action called userpage say, that will take the user's id and forward to the correct page.

Now, any url of the form localhost/myapp/userapps/anything.action will call the processURL method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜