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?
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.
This is how I do it:
Set up the view resolver so the view names are based on the request URL:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
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.
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>/WEB-INF/pages/*</url-pattern>
</servlet-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:
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</servlet>
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.
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"...
精彩评论