寻找Spring 3.0中更简洁的JSP转发配置

发布于 2024-10-17 14:40:17 字数 849 浏览 2 评论 0原文

在普通的 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 技术交流群。

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

发布评论

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

评论(3

贵在坚持 2024-10-24 14:40:17

我不完全理解你的问题,但是:

  • return "/something" 从控制器转发到名为 something.jsp 的 jsp (如果最典型的视图使用解析器配置)

  • 如果方法没有返回值,默认情况下会查找带有方法名称的 jsp。

I don't fully understand your problem, but:

  • return "/something" from a controller forwards to a jsp called something.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:

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

其次,servlet 容器根据匹配的最长路径选择映射。因此,您可以将此映射放入您的 JSP 中,并且它将优先于 /* 映射进行选择。

<servlet-mapping>
  <servlet-name>jsp</servlet-name>
  <url-pattern>/WEB-INF/pages/*</url-pattern>
</servlet-mapping>

事实上,对于 Tomcat,这就是您所需要的,因为 jsp 是一个开箱即用的 servlet。对于其他容器,您要么需要找出 JSP servlet 的名称,要么添加一个 servlet 定义,如下所示:

<servlet>
  <servlet-name>jsp</servlet-name>
  <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</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:

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

喵星人汪星人 2024-10-24 14:40:17

首先,无论您能从 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"...

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