将视图解析器添加到不同的请求

发布于 2024-10-18 09:31:50 字数 2477 浏览 5 评论 0原文

我读了很多关于 Spring 中 ViewResolvers 的文章,但我仍然有一个问题。我的部署描述符中有一个名为dispatcher的DispatcherServlet,它映射“/”所有URL以查看dispatcher-servlet.xml中的解析器。我现在只有一张。

<bean id="jspViewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp"/>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>

还有一个控制器:

@Controller
public class ItemsController {

@RequestMapping("/service")
public String service()
{
    System.out.println("From ItemsController...");
    return "service";
}

@RequestMapping("/")
public String home()
{
    System.out.println("From ItemsController...");
    return "index";
}
}

因此,如果请求 /service,jspViewResolver 将返回“WEB-INF/jsp/service.jsp”。没关系。但是如果我想将请求映射到 html 或 xhtml 文件。我需要做什么?添加另一个调度员?请帮我一下。预先非常感谢您。

I have read quite a few posts on ViewResolvers in Spring and I still have a question. I have a DispatcherServlet named dispatcher in my deployment descriptor and it maps "/" all URLs to view resolvers in dispatcher-servlet.xml. I have only one right now.

<bean id="jspViewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp"/>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>

And a controller:

@Controller
public class ItemsController {

@RequestMapping("/service")
public String service()
{
    System.out.println("From ItemsController...");
    return "service";
}

@RequestMapping("/")
public String home()
{
    System.out.println("From ItemsController...");
    return "index";
}
}

So jspViewResolver returns, in case /service is requested, "WEB-INF/jsp/service.jsp". That's fine. But if I want to map requests to html or xhtml files. What need I do? Add another dispatcher? Please, help me out here. Thank you so much in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

涙—继续流 2024-10-25 09:31:50

请参阅 15.5 节.2. Spring 文档的链接 ViewResolvers。这允许您使用多个 InternalResourceViewResolver 实例。


您还可以将其他视图解析器添加到您的 applicationContext 中,例如,

<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="someView" class="....."/>

然后在带注释的控制器中,当您返回字符串 "someView" 时,将使用具有相同 id 的 bean作为您的视图而不是您的 jsp 解析器。当名称冲突时,您必须使用 order 属性来设置哪个解析器具有优先权。

See section 15.5.2. Chaining ViewResolvers of the Spring documentation. This allows you to use multiple InternalResourceViewResolver instances.


You can also add other view resolvers to your applicationContext, e.g.,

<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="someView" class="....."/>

Then in your annotated controller, when you return the string "someView" the bean with the same id will be used as your view instead of your jsp resolver. When names are conflicting you have to use the order property to set which resolver gets precedence.

残月升风 2024-10-25 09:31:50

解决了。

htmlsolver.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<% response.sendRedirect("/rantservice/newhtml.html"); %>

web.xml

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/hope/*</url-pattern>
</servlet-mapping>

Solved.

htmlsolver.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<% response.sendRedirect("/rantservice/newhtml.html"); %>

web.xml

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