【问题标题】:Cannot make multiple view resolvers in spring mvc无法在spring mvc中创建多个视图解析器
【发布时间】:2015-04-07 16:02:28
【问题描述】:

我想为我尝试过多次的 .jsp 和 .xhtml 页面创建 2 个视图解析器,但过程失败了 我将 .jsp 视图解析器作为最高优先级的视图解析器 它适用于 .jsp 页面,但是当涉及 .xhtml 页面时,它给了我一个错误:找不到资源 因此,如果找不到“.jsp”视图,它不会处理“.xhtml”视图解析器 这是错误的屏幕截图和项目架构: http://postimg.org/image/5cz5iulwd/

下面是 web.xml servle-context.xml 和 applicationContext.xml 代码 我该怎么办?

web.xml -->

<!-- Spring Security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<!-- xhtml mapping -->
<servlet> 
    <servlet-name>FacesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>FacesServlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

applicationContext.xml --->

enter code here <!-- datasource -->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/primefaces"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
</bean>
<!-- persistenceUnitManager -->
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="defaultDataSource" ref="datasource"/>
    <property name="persistenceXmlLocations">
        <list>
            <value>classpath*:META-INF/persistence.xml</value>
        </list>
    </property>
</bean>
<!-- entityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitManager" ref="persistenceUnitManager"/>
    <property name="persistenceUnitName" value="UP_TEST"/>
</bean>
<!-- transactionManager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:annotation-config/>
<bean id="dao" class="com.test.app.dao.DaoIml"></bean>
<bean id="metier" class="com.test.app.metier.MetierIml">
    <property name="dao" ref="dao"></property>
</bean>
<s:http  use-expressions="true">
    <s:intercept-url pattern="/admin/**" access="ROLE_ADMIN"/>
    <s:form-login login-page="/login" default-target-url="/default" authentication-failure-url="/login" />
    <s:logout logout-success-url="/login" />
</s:http>
<s:authentication-manager>
    <s:authentication-provider>
        <s:password-encoder hash="md5"></s:password-encoder>
        <s:jdbc-user-service data-source-ref="datasource" users-by-username-query="select user_name,password, actived from users where user_name=?" authorities-by-username-query="select u.user_name, r.roleName from users u, role r where u.user_id = r.user_id and u.user_name =? " />
    </s:authentication-provider>
</s:authentication-manager>

这里是 servlet-context.xml

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

 <beans:bean id="viewResolver"
   class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></beans:property>
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" /> 
<beans:property name="order" value="0" />
</beans:bean>


<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".xhtml" />
      <beans:property name="order" value="1" />
</beans:bean>

<context:component-scan base-package="com.test.app" />

【问题讨论】:

  • 一个问题,为什么不将您的 xhtml 文件作为静态资源提供?

标签: spring jsp spring-mvc spring-security xhtml


【解决方案1】:

看起来您不能将 InternalResourceViewResolver 和 UrlBasedViewResolver 链接在一起。 InternalResourceViewResolver 扩展了 UrlBasedViewResolver。博客“blog.frankel.ch/chaining-url-view-resolvers-in-spring-mvc”;可以给你一个选择

【讨论】:

  • 尝试将 xhtml 视图解析器(基于 url)更改为 0,将 jsp 视图解析器(内部视图解析器)更改为 1,它告诉我找不到“login.xhtml " 文件(我想打开)并给了我这个错误:s7.postimg.org/cu2b4ld0r/err.png
  • 我看不到屏幕截图中的错误。您可以在此处发布其他图片或错误。
  • 看起来你不能将 InternalResourceViewResolver 和 UrlBasedViewResolver 链接在一起。 InternalResourceViewResolver 扩展了 UrlBasedViewResolver。博客“blog.frankel.ch/chaining-url-view-resolvers-in-spring-mvc”可以给你一个选择。希望这会有所帮助。
猜你喜欢
  • 2011-01-18
  • 2022-11-16
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
  • 1970-01-01
  • 2020-05-17
  • 1970-01-01
相关资源
最近更新 更多