将视图解析器添加到不同的请求
我读了很多关于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅 15.5 节.2. Spring 文档的链接 ViewResolvers。这允许您使用多个
InternalResourceViewResolver
实例。您还可以将其他视图解析器添加到您的
applicationContext
中,例如,然后在带注释的控制器中,当您返回字符串
"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.,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 theorder
property to set which resolver gets precedence.解决了。
htmlsolver.jsp
web.xml
Solved.
htmlsolver.jsp
web.xml