Spring MVC 4.0+freemarker Cast javax.servlet.ServletException:Could not resolve view with name

Source: Internet
Author: User
Tags aop

Spring 4.0+spring Mvc4.0+freemarker Integration project, everything is all right, but javax.servlet.ServletException:Could not resolve view with name appears The problem. Here is a foreshadowing, first look at my integrated configuration:

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>

<!--load Spring container configuration-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--set the spring container load configuration file path-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/autoclub/config/spring.xml</param-value>
</context-param>

<!--Configure the Spring core controller-->
<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>
<!--Solution Engineering Coding Filter-->
<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 ">

<!--activate @controller mode-->
<mvc:annotation-driven/>

<!--scan all classes in the package to complete the bean creation and automatic dependency injection functionality needs to be changed-->
<context:component-scan base-package= "Com.autoclub"/>

<!--introduce JDBC configuration file-->
<context:property-placeholder location= "Classpath:com/autoclub/config/jdbc.properties"/>

<!--Create a JDBC data source-->
<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>

<!--things annotation-->
<tx:annotation-driven/>

<!--configuration transaction manager-->
<bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource"/>
</bean>

The propagation characteristics of <!--transactions-->
<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>

<!--Configure the Spring declarative Transaction service Scope-->
<aop:config>
<aop:pointcut id= "Bussinessservice" expression= "Execution (public * com.autoclub.service.*.* (..))"/>
<aop:advisor pointcut-ref= "Bussinessservice" advice-ref= "Txadvice"/>
</aop:config>

<!--Create Sqlsessionfactory and specify the data source-->
<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<property name= "DataSource" ref= "DataSource"/>
<property name= "configlocation" value= "Classpath:com/autoclub/config/mybatis.xml"/>
<!--mapper and Resultmap configuration Paths-->
<property name= "Mapperlocations" >
<list>
<!--represents all files at the end of the Com/autoclub/domain/mapper package or all of the following directories-resultmap.xml-->
<value>classpath:com/autoclub/domain/mapper/**/*.xml</value>
</list>
</property>
</bean>

<!--configuration Sqlsession-->
<bean id= "sqlsession" class= "Org.mybatis.spring.SqlSessionTemplate" >
<constructor-arg index= "0" ref= "sqlsessionfactory"/>
</bean>

<!--the package name of the Mapper interface, Spring automatically finds the mapper--> under it
<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name= "Basepackage" value= "Com.autoclub.domain.mapper"/>
</bean>

<!--error interceptor, unified processing
<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 The default method mapping adapter-->
<bean id= "handlermapping" class= " Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping "/>
<bean id= "Handleradapter" class= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "/>

<!--file Upload related-->
<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 View resolution if return student ... Here, configure the suffix name FTL and the view parser. -->
<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>


Test example 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", "Xiao Chen");
Modelandview.setviewname ("Admin/registeruser");
return modelandview;
}

@RequestMapping ("/test")
Public String Test (Modelmap map) {
System.out.println ("222in22221");
return "Admin/registeruser";
}
}

Everything is so beautiful, feel that there is no problem. Then IE on the knock address: Http://localhost:8080/AUTOCLUB/admin/user/test2

Background Direct Throw
Looking for a morning, the company all the cattle have been called to see an article, all feel the configuration is no problem, the method is also written. Well, finally there is no way, OK, return to the most basic, doubt whether the method to write a problem, check the introduction of Modelandview objects, found that the introduction of the Spring-webmvc-portlet-4.0.0.release.jar packet under the Org.springframework.web.portlet.ModelAndView, this error, temporarily do not know why the enhanced package provided by the object will have Problems, change back to Org.springframework.web.servlet.ModelAndView after the introduction of problem solving . If a friend knows why please leave a message for me, thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.