如何将欢迎页面设置为 struts 操作?
我有一个基于 struts 的 web 应用程序,我希望默认的“欢迎”页面成为一个操作。 我发现的唯一解决方案似乎是使欢迎页面成为包含操作重定向的 JSP。 例如,在 web.xml
中:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
和在 index.jsp
中:
<%
response.sendRedirect("/myproject/MyAction.action");
%>
当然有更好的方法!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
我几乎确信OP是最好的解决方案(不确定最佳实践,但它工作完美,实际上是我的项目负责人和我更喜欢的解决方案。)
此外,我发现它可以与Spring安全性结合起来,如下所示:
通过这种方式,我们不仅可以控制作为登录表单的第一页,而且可以根据用户的角色控制用户登录后的流程。 奇迹般有效。
I am almost sure that the OP is the best solution(not sure about best practice, but it works perfectly, and actually is the solution my project leader and I prefer.)
Additionally, I find it can be combined with Spring security like this:
By this, not only we have control over the first page to be the login form, but we control the flow AFTER user is login in, depending on his role. Works like a charm.
这对我来说也很好:
当用户使用 Web 应用程序的根目录 (mywebapp/) 进入 Web 应用程序时,我无法获得要执行的默认操作。 struts 2.3.12 中有一个 bug,当您使用 root url 时,不会进入默认操作或使用欢迎页面。 这将是一个常见的现象。 一旦我改回struts 2.1.8,它就工作得很好。
This worked fine for me, too:
I was not able to get the default action to execute when the user enters the webapp using the root of the web app (mywebapp/). There is a bug in struts 2.3.12 that won't go to the default action or use the welcome page when you use the root url. This will be a common occurrence. Once I changed back to struts 2.1.8 it worked fine.
这也可以减少对新 servlet 或 jsp 的需求
This works as well reducing the need of a new servlet or jsp
我的配置如下。 它工作完美,也没有 URL 更改...
创建一个虚拟操作,如 struts2.xml 文件中的以下内容。 因此,每当我们访问像
http://localhost:8080/myapp
这样的应用程序时,它都会将其转发到虚拟操作,然后重定向到index.jsp/index.tiles...没有瓷砖
可能 我们在 web.xml 中配置一些动作index.action为
index.action
,并使用该动作转发所需的页面...I have configured like following. it worked perfect and no URL change also...
Create a dummy action like following in struts2.xml file. so whenever we access application like
http://localhost:8080/myapp
, it will forward that to dummy action and then it redirects to index.jsp / index.tiles...w/o tiles
may be we configure some action index.action in web.xml as
<welcome-file>index.action</welcome-file>
, and use that action to forward required page...下面的代码可以在struts.xml中使用来加载欢迎页面。
在加载欢迎页面之前执行一些操作。
直接返回一些 JSP,而不执行 Action。
web.xml 中不需要
Below code can be used in struts.xml to load welcome page.
Execute some Action before loading a welcome page.
Return directly some JSP without execution of an Action.
No
<welcome-file-list>
is not needed in web.xml上面有这个答案,但不清楚网络应用程序上下文
所以
我这样做:
在
index.jsp
中,我只是写:我有索引定义,名为
index
,它一起工作正常,不依赖于 webapp 上下文路径。there are this answer above but it is not clear about web app context
so
i do this:
And in
index.jsp
i just write:And i have index definition, named
index
and it all togather work fine and not depends on webapp context path.这里有两个具有相同技术的博客:
它需要 Servlet API >= v2.4:
因此您不再需要非
WEB-INF
中的redirect.jsp
代码>目录!!Here two blogs with same technique:
It require Servlet API >= v2.4:
so you no longer need
redirect.jsp
in non-WEB-INF
directory!!看来流行的解决方案并不适用于所有容器...... http:// /www.theserverside.com/discussions/thread.tss?thread_id=30190
It appears that a popular solution will not work in all containers... http://www.theserverside.com/discussions/thread.tss?thread_id=30190
我将创建一个过滤器并通过转发响应将所有请求退回 root。 创建 home.do 页面的黑客对我来说看起来很丑(你还需要记住一件事,并为支持你的代码的人进行调查)。
I would create a filter and bounce all requests to root back with forward responce. Hacks with creating home.do page looks ugly to me (One more thing to remember for you and investigate for someone who will support your code).
只需在
web.xml
中的 Strut 过滤器上方添加一个过滤器,如下所示:并在 CustomFilter 类的 doFilter 方法中添加以下代码,
这样 Filter 就会重定向到该操作。 您也不需要将任何 JSP 放置在 WEB-INF 之外。
Just add a filter above Strut's filter in
web.xml
like this:And add the following code in doFilter method of that CustomFilter class
So that Filter will redirect to the action. You dont need any JSP to be placed outside WEB-INF as well.
我所做的就是放置一个与 struts 操作同名的空文件,并欺骗容器调用 struts 操作。
前任。 如果您的 struts 操作是welcome.do,请创建一个名为welcome.do 的空文件。 这应该会欺骗容器调用 Struts 操作。
Something that I do is to put an empty file of the same name as your struts action and trick the container to call the struts action.
Ex. If your struts action is welcome.do, create an empty file named welcome.do. That should trick the container to call the Struts action.
“当然还有更好的办法!”
没有。 Servlet 规范(Java Servlet 规范 2.4,例如“SRV.9.10 欢迎文件”)状态:
您无法将 Struts 映射到“/”,因为 Struts 需要使用文件扩展名。 因此,您只能使用隐式映射的组件,例如 JSP 或静态文件。 所有其他解决方案都只是黑客。 因此,保留您的解决方案,它是完全可读和可维护的,不必费心进一步寻找。
"Surely there's a better way!"
There isn't. Servlet specifications (Java Servlet Specification 2.4, "SRV.9.10 Welcome Files" for instance) state:
You can't map Struts on '/', because Struts kind of require to work with a file extension. So you're left to use an implicitely mapped component, such as a JSP or a static file. All the other solutions are just hacks. So keep your solution, it's perfectly readable and maintainable, don't bother looking further.
从 Servlet 规范 2.4 版本开始,您可以在欢迎文件列表中包含 servlet。 请注意,这可能不是 URL(例如 /myproject/MyAction.action)。 它必须是一个命名的 servlet,并且您不能将查询字符串传递给该 servlet。 您的控制器 servlet 需要有一个默认操作。
As of the 2.4 version of the Servlet specification you are allowed to have a servlet in the welcome file list. Note that this may not be a URL (such as /myproject/MyAction.action). It must be a named servlet and you cannot pass a query string to the servlet. Your controller servlet would need to have a default action.
这是一个相当古老的线程,但我认为讨论的主题仍然相关。 我使用 struts 标签 - s:action 来实现这一点。 我创建了一个index.jsp,在其中写了这个......
This is a pretty old thread but the topic discussed, i think, is still relevant. I use a struts tag - s:action to achieve this. I created an index.jsp in which i wrote this...
就我个人而言,我会保留您现在的相同设置,但更改转发的重定向。 这可以避免将标头发送回客户端并让他们发出另一个请求。
因此,我特别将
index.jsp 中的 替换为 此
更改的另一个效果是用户不会看到地址栏中的 URL 从“http://server/myproject" 到“http://server/ myproject/index.jsp”,因为转发发生在服务器内部。
Personally, I'd keep the same setup you have now, but change the redirect for a forward. That avoids sending a header back to the client and having them make another request.
So, in particular, I'd replace the
in index.jsp with
The other effect of this change is that the user won't see the URL in the address bar change from "http://server/myproject" to "http://server/myproject/index.jsp", as the forward happens internally on the server.