寻找Spring 3.0中更简洁的JSP转发配置
在普通的 Java weba 应用程序中,如果我将其放入 servlet 代码中,则转发将起作用:
getServletConfig().getServletContext().getRequestDispatcher("/something.jsp").forward(req, resp);
但是当我在 Spring 3.0 应用程序中的同一个 servlet 中执行此操作时,即使我添加此条目,我也会得到 404
到我的应用程序上下文 xml 文件:
<intercept-url pattern="/something.jsp**" access="hasRole('ROLE_ANONYMOUS')" requires-channel="http" />
相反,我似乎必须在 Spring 中执行此操作:
getServletConfig().getServletContext().getRequestDispatcher("/something").forward(req, resp);
并在控制器中添加映射:
@RequestMapping(value = {"/something"}, method = RequestMethod.GET)
public final String something(HttpServletRequest req, ModelMap model) {
...
}
但是,要使简单的 JSP 正常工作,这是一个相当重要的绕道。
是有更好的方法吗?
In a normal Java weba app, if I put this in my servlet code the forwarding works:
getServletConfig().getServletContext().getRequestDispatcher("/something.jsp").forward(req, resp);
But when I do this in the same servlet in a Spring 3.0 app I get a 404
even if I add this entry to my application context xml file:
<intercept-url pattern="/something.jsp**" access="hasRole('ROLE_ANONYMOUS')" requires-channel="http" />
Instead I have to do this in Spring it seems:
getServletConfig().getServletContext().getRequestDispatcher("/something").forward(req, resp);
and add a mapping in the controller:
@RequestMapping(value = {"/something"}, method = RequestMethod.GET)
public final String something(HttpServletRequest req, ModelMap model) {
...
}
But this is quite a significant detour to get a simple JSP forward to work.
Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不完全理解你的问题,但是:
return "/something"
从控制器转发到名为something.jsp
的 jsp (如果最典型的视图使用解析器配置)如果方法没有返回值,默认情况下会查找带有方法名称的 jsp。
I don't fully understand your problem, but:
return "/something"
from a controller forwards to a jsp calledsomething.jsp
(if the most typical view resolver configuration is used)if you don't have a return value from a method, by default a jsp with the method name is looked up.
我是这样做的:
设置视图解析器,以便视图名称基于请求 URL:
其次,servlet 容器根据匹配的最长路径选择映射。因此,您可以将此映射放入您的 JSP 中,并且它将优先于 /* 映射进行选择。
事实上,对于 Tomcat,这就是您所需要的,因为 jsp 是一个开箱即用的 servlet。对于其他容器,您要么需要找出 JSP servlet 的名称,要么添加一个 servlet 定义,如下所示:
一旦这两件事就位,那么除了返回模型之外,您不需要在控制器中执行任何操作。它会根据您请求的 URL 自动转发到 WEB-INF/pages 中的视图。在您的示例中,它将是/WEB-INF/pages/something.jsp。
This is how I do it:
Set up the view resolver so the view names are based on the request URL:
Second, the servlet container chooses the mapping based on the longest path that matches. So you can put this mapping in for your JSPs and it will be chosen over the /* mapping.
Actually for Tomcat that's all you'll need since jsp is a servlet that exists out of the box. For other containers you either need to find out the name of the JSP servlet or add a servlet definition like:
Once those two things are in place then you don't need to do anything in your controllers except return a model. It will automatically forward to the view from WEB-INF/pages based on the URL of your request. In your example it would be /WEB-INF/pages/something.jsp.
首先,无论您能从 servlet 执行什么操作,您都可以在 Spring MVC 控制器中执行(此时您基本上位于
DispatcherServlet.service()
内),所以如果您得到 404 这可能与您的 servlet 映射有关。您是否有可能将 Dispatcher servlet 映射为 /* ?另外(但更重要的是,我想说),Spring MVC(基本上与任何 Web 框架一样)应该对您隐藏 servlet 基础结构,因此我不太清楚您需要使用 RequestDispatcher 并转发请求。
要将请求转发到 JSP,只需将其放在 ViewResolver 知道的位置,然后从控制器方法返回“某些内容”——因为这具有完全相同的效果,并且更“MVC 风格”...
First of all, whatever you are able to do from a servlet you can do from within a Spring MVC controller (as at that point you're basically inside the
DispatcherServlet.service()
), so if you get a 404 this may be related to your servlet mapping. Is it possible that you mapped the Dispatcher servlet as /* ?Separately (but more importantly, I'd say), Spring MVC (as basically any web framework) is supposed to hide the servlet infrastructure from you, therefore your need to use a RequestDispatcher and forward a request is not very clear to me.
To forward a request to a JSP, simply have that in a location that the ViewResolver knows about and just return "something" from the controller method -- as this has the exact same effect and is more "MVC-ish"...