How to use interceptor on the default page?

百般思念 提交于 2019-12-13 01:38:11

问题


Here's my situation : in the webapp, I use an interceptor to set the language(Locale). If a user is logged, I used the language property of this user. Else if a cookie is set, I use the value of this cookie. Else, I use the setting of the browser.

It works well when I navigate into the app and when I am logged.

The problem is at the welcome page, since it calls mydomain.com/index.jsp, it don't go through the interceptors so the language isn't set(it's always using the browser setting).

Is there a way to go through the interceptors on the index page or to set the Locale value in the index.jsp page ?

Thank you!

The solution :

I removed the .jsp from the index.jsp in the web.xml file :

<welcome-file-list>
  <welcome-file>index</welcome-file>
</welcome-file-list>

I added the index action to my struts.xml file :

<default-action-ref name="index" />

<action name="index">
  <interceptor-ref name="appStack" />
  <result name="success">index.jsp</result>
</action>

The language interceptor is part of the appStack.

Thank you guys for your helps!


回答1:


I would just add the conventions plugin (struts2-conventions-plugin-x.x.x.jar) where x.x.x is the version you are using.

Then I would move all the public jsp's under /WEB-INF/content and be done.

In your web.xml I don't mention any welcome files... but if you would like to be explicit:

<welcome-file-list>
    <welcome-file>index</welcome-file>
</welcome-file-list>

If using Struts 2.2.1 your web.xml should minimally look like...

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
    <filter-name>action</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>action</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

If you want to do it the struts.xml way then just move the index.jsp under /WEB-INF and create an action mapping for it... something like:

    <action name="index">
        <result>/WEB-INF/index.jsp</result>
    </action>

which would be in a package with namespace "/" or "".

On the other hand I haven't looked at the Local stuff in a while but are you sure you're not reinventing the wheel... the i18n interceptor is already in the default stack.

Look into it's use. Long story short there are language property files defined for each language. If Struts2 has determined such properties are in use then then the struts tags will search the value in its name attribute for a matching string in the property file and return the value of that map entry.

It all works pretty slick. Sorry if this is what you are already doing but on the chance you didn't know it should save you a lot of time.




回答2:


I recommend that you do one of two things: (your choice)

(1) Implement your logic in a web filter and have your container configured with this filter so you can set the language (if not already set). This is easy to do, just look at any example of a HelloWorld filter.

or...

(2) Make sure that your home page is only reachable as a Struts2 action (you can define a default action in your Struts2 config file) and ensure that your interceptor is part of the default stack.

Hope that helps!



来源:https://stackoverflow.com/questions/4522350/how-to-use-interceptor-on-the-default-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!