在spring mvc中无法制作多个视图解析器

时间:2015-02-07 15:31:53

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

我想为.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 ---&gt;

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

1 个答案:

答案 0 :(得分:1)

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