如何在默认页面使用拦截器?

发布于 2024-10-08 20:31:32 字数 876 浏览 0 评论 0原文

这是我的情况:在网络应用程序中,我使用拦截器来设置语言(Locale)。 如果用户已登录,我使用该用户的语言属性。 否则,如果设置了 cookie,我将使用该 cookie 的值。 否则,我使用浏览器的设置。

当我导航到应用程序和登录时,它运行良好。

问题出在欢迎页面,因为它调用 mydomain.com/index.jsp,所以它不会通过拦截器,因此未设置语言(它始终使用浏览器设置)。

有没有办法通过索引页面上的拦截器或在index.jsp页面中设置Locale值?

谢谢你!

解决方案:

我从 web.xml 文件中的 index.jsp 中删除了 .jsp

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

我添加了 index 操作到我的 struts.xml 文件:

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

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

语言拦截器是 appStack 的一部分。

谢谢你们的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相权↑美人 2024-10-15 20:31:32

我建议您执行以下两件事之一:(您的选择)

(1) 在 Web 过滤器中实现您的逻辑,并使用此过滤器配置您的容器,以便您可以设置语言(如果尚未设置)。这很容易做到,只需查看 HelloWorld 过滤器的任何示例即可。

或者...

(2) 确保您的主页只能作为 Struts2 操作访问(您可以在 Struts2 配置文件中定义默认操作),并确保您的拦截器是默认堆栈的一部分。

希望有帮助!

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!

浪菊怪哟 2024-10-15 20:31:32

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

然后我将所有公共 jsp 移动到 /WEB-INF/content 下并完成。

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

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

如果使用 Struts 2.2.1 你的 web.xml 至少应该看起来像...

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

如果您想以 struts.xml 方式执行此操作,则只需将 index.jsp 移到 /WEB-INF 下并为其创建一个操作映射...类似于:

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

它将位于具有名称空间“/”或“”的包中。

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

看看它的用途。长话短说,为每种语言定义了语言属性文件。如果 Struts2 确定正在使用此类属性,那么 struts 标记将在其 name 属性中搜索属性文件中匹配的字符串,并返回该映射条目的值。

这一切都运行得非常流畅。很抱歉,如果这是您已经在做的事情,但您可能不知道它会节省您很多时间。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文