Spring mvc 4.0+freemarker拋javax.servlet.ServletException: Could not resolve view with name

來源:互聯網
上載者:User

Spring 4.0+Spring mvc4.0+Freemarker整合項目,一切的東西都配好了,但是出現javax.servlet.ServletException: Could not resolve view with name 的問題。這裡先留一個伏筆,先看看我的整合配置:

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">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:com/autoclub/config/log4j.properties</param-value>
    </context-param>
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>6000</param-value>
    </context-param>
    <listener>
      <listener-class>
        org.springframework.web.util.Log4jConfigListener
      </listener-class>
   </listener>

    <!-- 載入Spring容器配置 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 設定Spring容器載入設定檔路徑 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:com/autoclub/config/spring.xml</param-value>
    </context-param>
    
    <!-- 配置Spring核心控制器 -->
    <servlet>
        <servlet-name>dispatcher1</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:com/autoclub/config/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher1</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- 解決工程編碼過濾器 -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

Spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns:aop="http://www.springframework.org/schema/aop"  
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.2.xsd">

       <!-- 啟用@Controller模式 -->
    <mvc:annotation-driven/>  
        
    <!-- 對包中的所有類進行掃描,以完成Bean建立和自動依賴注入的功能 需要更改 -->
    <context:component-scan base-package="com.autoclub"/>
    
    <!-- 引入jdbc設定檔 -->
    <context:property-placeholder location="classpath:com/autoclub/config/jdbc.properties" />

    <!--建立jdbc資料來源 -->
    <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/autoclub" />
        <property name="username" value="root" />
        <property name="password" value="123" />
    </bean>

    <!-- 事物註解 -->
    <tx:annotation-driven />

    <!-- 配置交易管理員 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- 事務的傳播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 配置spring聲明式事務服務涵蓋範圍 -->
    <aop:config>
        <aop:pointcut id="bussinessService" expression="execution(public * com.autoclub.service.*.*(..))" />
        <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
    </aop:config>

    <!-- 建立SqlSessionFactory,同時指定資料來源 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:com/autoclub/config/mybatis.xml"/>       
         <!-- mapper和resultmap配置路徑 -->     
        <property name="mapperLocations">        
            <list>           
                <!-- 表示在com/autoclub/domain/mapper包或以下所有目錄中,以-resultmap.xml結尾所有檔案 -->     
                <value>classpath:com/autoclub/domain/mapper/**/*.xml</value>            
            </list>        
        </property>
    </bean>

    <!-- 配置SqlSession -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
    
    <!-- Mapper介面所在包名,Spring會自動尋找其下的Mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.autoclub.domain.mapper" />
    </bean>
    
    <!-- 錯誤攔截器,統一處理
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="org.apache.shiro.authc.IncorrectCredentialsException">redirect:index.html</prop>
            </props>
        </property>
        <property name="defaultErrorView">
            <value>error</value>
        </property>
    </bean> -->
 
</beans>


Spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <!--  annotation預設的方法映射適配器 -->
    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    
    <!-- 檔案上傳相關 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--one of the properties available;the maximum file size in bytes-->
        <property name="maxUploadSize" value="100000"/>
    </bean>
    
    <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/template/" />
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">10</prop>
                <prop key="locale">zh_CN</prop>
                <prop key="datetime_format">yyyy-MM-dd</prop>
                <prop key="date_format">yyyy-MM-dd</prop>
                <prop key="number_format">#.##</prop>
                <prop key="default_encoding">utf-8</prop>
            </props>
        </property>
    </bean>
    
    <!-- FreeMarker視圖解析   如返回student。。在這裡配置尾碼名ftl和視圖解析器。。-->
     <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property>
        <property name="prefix" value="/"/>
        <property name="suffix" value=".ftl" />
        <property name="contentType" value="text/html;charset=utf-8" />
        <property name="exposeRequestAttributes" value="true" />
        <property name="exposeSessionAttributes" value="true" />
        <property name="exposeSpringMacroHelpers" value="true" />
     </bean>
    
     <!-- json view -->
    <bean id="defaultJsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
</beans>


測試例子Controller

package com.autoclub.controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;



@Controller
@RequestMapping("/admin/user")
public class UserController {
    
    @RequestMapping("/test2")
    public ModelAndView initCreate(){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("111in1111");
        modelAndView.addObject("user","小陳");
        modelAndView.setViewName("admin/registerUser");
        return modelAndView;
    }
    
    @RequestMapping("/test")
    public String test(ModelMap map){
        System.out.println("222in22221");
        return "admin/registerUser";
    }
}

一切都那麼的美好,覺得沒有什麼問題。然後IE上敲地址:http://localhost:8080/AUTOCLUB/admin/user/test2

後台直接拋
找了一個早上,把公司所有的牛人都叫過來看了一篇,都覺得配置沒有問題,方法也是這樣寫的。好吧,最後沒有辦法了,好吧,迴歸最基礎,懷疑是否方法寫得有問題,檢查 ModelAndView對象的引入,發現引入的是spring-webmvc-portlet-4.0.0.RELEASE.jar包下的org.springframework.web.portlet.ModelAndView,出現這個錯誤,暫時不知道為什麼這個增強包提供的對象就會有問題,換回org.springframework.web.servlet.ModelAndView引入後 問題解決。如果有朋友知道原因 請給我留言,謝謝。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.