English 中文(简体)
如何在Struts 2中解析请求URL?
原标题:How to parse Request URL in Struts 2?

我的网站应用程序(使用struts2创建)中注册的账户会获得一个独立的站点,如http://localhost/accountname

每个帐户都有自己的登录页面。注册后,用户将获得一个单独的网站http://localhost/accountname

我想首先扫描请求的URL(在struts2中)然后将该请求转发到相应的登录页面。

我该怎么做?请帮忙。

最佳回答

您可以通过以下方式在您的操作方法中获取请求对象:

   HttpServletRequest request = ServletActionContext.getRequest();

您可以通过如下方式找到请求的URL:

   String spath = request.getServletPath();

然后您可以解析它并寻找您所需要的模式并相应地进行转发。


更新:

你们可以在休战队编队档案中使用包裹。 让我们说“使用者”。

     <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>

在 ActionClass 的 processURL 方法中,您可以提取您感兴趣的 URL 部分,并设置一个属性,例如用户和他的 ID。然后,您可以从您的 action 返回 success。

您将拥有第二个动作,称为用户页面,它将获取用户的ID并转发到正确的页面。

现在,任何形式为localhost/myapp/userapps/anything.action的URL都将调用processURL方法。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签