如何将欢迎页面设置为struts操作?

mro*_*owe 40 java jsp struts struts2

我有一个基于struts的webapp,我希望默认的"欢迎"页面是一个动作.我发现的唯一解决方案似乎是使欢迎页面成为包含重定向到操作的JSP的变体.例如,在web.xml:

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
Run Code Online (Sandbox Code Playgroud)

并在index.jsp:

<% 
  response.sendRedirect("/myproject/MyAction.action");
%> 
Run Code Online (Sandbox Code Playgroud)

当然有更好的方法!

Mar*_*lty 23

就个人而言,我会保持现在的相同设置,但更改前进的重定向.这样可以避免将标题发送回客户端并让他们发出另一个请求.

所以,特别是,我会替换

<% 
  response.sendRedirect("/myproject/MyAction.action");
%>
Run Code Online (Sandbox Code Playgroud)

在index.jsp中

<jsp:forward page="/MyAction.action" />
Run Code Online (Sandbox Code Playgroud)

此更改的另一个影响是,用户将看不到地址栏中的URL从" http:// server/myproject "更改为" http://server/myproject/index.jsp ",因为转发正在发生在服务器上内部.

  • 当sendRedirect工作时,forward对我不起作用.得到'404 Not'. (4认同)

Sri*_*nth 17

这是一个非常古老的主题,但我认为讨论的主题仍然具有相关性.我使用struts标签 - s:action来实现这一点.我创建了一个index.jsp,其中我写了这个...

<s:action name="loadHomePage" namespace="/load" executeResult="true" />
Run Code Online (Sandbox Code Playgroud)


Cra*_*eil 11

从Servlet规范的2.4版本开始,您可以在欢迎文件列表中拥有一个servlet.请注意,这可能不是URL(例如/myproject/MyAction.action).它必须是命名的servlet,并且您不能将查询字符串传递给servlet.您的控制器servlet需要具有默认操作.

<servlet>
  <servlet-name>MyController</servlet-name>
  <servlet-class>com.example.MyControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>MyController</servlet-name>
  <url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
  <welcome-file>MyController</welcome-file>
</welcome-file-list>
Run Code Online (Sandbox Code Playgroud)


Dam*_*n B 6

"当然有更好的方法!"

没有.Servlet规范(例如Java Servlet规范2.4,"SRV.9.10欢迎文件")状态:

此机制的目的是允许部署者在请求与WAR中未映射到Web组件的WAR中的目录条目的URI时,为容器指定用于附加到URI的部分URI的有序列表. .

你不能在'/'上映射Struts,因为Struts需要使用文件扩展名.因此,您只能使用隐含映射的组件,例如JSP或静态文件.所有其他解决方案都只是黑客攻击.所以保持你的解决方案,它是完全可读和可维护的,不要再费心了.


Nis*_*hal 6

我所做的就是放置一个与struts动作同名的空文件,并欺骗容器来调用struts动作.

防爆.如果您的struts操作是welcome.do,请创建一个名为welcome.do的空文件.这应该欺骗容器调用Struts动作.


bpa*_*apa 1

看来流行的解决方案并不适用于所有容器...... http://www.theserverside.com/discussions/thread.tss?thread_id=30190