如何在默认页面上使用拦截器? [英] How to use interceptor on the default page?

查看:129
本文介绍了如何在默认页面上使用拦截器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的情况:在webapp,我使用拦截器设置语言( Locale )。
如果用户已登录,我使用了此用户的language属性。
否则如果设置了一个cookie,我使用这个cookie的值。
否则,我使用浏览器的设置。



当我导航到应用程序和我登录时,它工作得很好。



问题是在欢迎页面,因为它调用mydomain.com/index.jsp,它不通过拦截器,所以语言没有设置(它总是使用浏览器设置)。



有一种方法可以通过索引页上的拦截器或者设置 Locale

/ p>

我从 index.jsp 中删除​​了 .jsp 在web.xml文件中:

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

我添加了索引 .xml文件:

 < default-action-ref name =index/> 

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

语言拦截器是 appStack



感谢您的帮助!

解决方案

只需添加约定plugin(struts2-conventions-plugin-xxxjar),其中xxx是您正在使用的版本。



然后我将所有的公共jsp都移到/ WEB-INF /内容下面。



你的web.xml我没有提到任何欢迎文件...但如果你想要明确:

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

如果使用Struts 2.2.1,你的web.xml应该最低限度看起来像...

 < web-app version =2.5xmlns =http://java.sun.com/xml/ns/javaeexmlns :xsi =http://www.w3.org/2001/XMLSchema-instancexsi: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>



struts.xml方法然后只是移动index.jsp在/ WEB-INF下,并为它创建一个操作映射...像:

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

这将在包含命名空间/或的包中。



另一方面,我没有看过Local的东西在一段时间,但你确定你不是重新发明轮子... i18n拦截器已经在默认堆栈。



看看它的用途。长篇小说有为每种语言定义的语言属性文件。如果Struts2确定这样的属性正在使用,那么struts标签将搜索属性文件中匹配字符串的name属性中的值,并返回该映射条目的值。



这一切都很漂亮。对不起,如果这是你已经在做,但机会你不知道它应该可以节省你很多时间。


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!

解决方案

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.

这篇关于如何在默认页面上使用拦截器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆