spring4.2 full Web project (using HTML view parser)

Source: Internet
Author: User

Full configuration Springmvc4, the final view is selected for HTML, non-static files.

Recently configured spring, encountered a lot of problems, due to the development environment and version of the changes caused netizens to give a lot of suggestions or can not be used, there may be a lot of people will encounter the same problems with me, hoping to help the people meet difficulties.

Environment: JDK8,SPRING4.2.0,TOMCAT8, using IDEA14 Editor, maven management, git version controller, project remote Library address Github:https://github.com/627135316/on_line. Welcome everyone clone. The middle user name is also my QQ number.

The basic environment is not long-winded, this online information is generally very full. The following will introduce a lot of details of things, is my own setting up the environment when the headache encountered, mainly for the first time to build their own, no experience. Don't laugh, huh? I'll just post the code. The information about Maven I put on the last side, because there might be packages you don't need.

This is the file directory for this project.

  

Next go to the famous web. Xml. The configuration on the web is similar. In the directory I prioritized the Spring listener--contextloaderlistener, which I previously configured under the servlet tag of the file, but has been error-tested and tried a lot of ways. Some people say that the listener class configuration is to allow the controller to intercept the URL address request, in fact, nothing, I do not configure the monitoring when the controller can still go in. There are two additional XML files configured in Web.

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://xmlns.jcp.org/xml/ns/javaee/http Xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "version=" 3.1 "> <!--Load Spring container configuration, configure listener--<listene R> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </li Stener> <!--set Spring container Load profile path--<context-param> <param-name>contextconfiglocation&lt ;/param-name> <param-value>/WEB-INF/config/applicationContext.xml</param-value> </context-para m> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springfram Ework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextco      Nfiglocation</param-name>      <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-sta rtup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</ser Vlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>

Next comes the applicationcontext.xml. Here is the basic configuration of the freemarker, there is no mistake, we are using Freemarker as an HTML parser. Many people on the internet are org.springframework.web.servlet.view.InternalResourceViewResolver this class, it can only parse JSP, and its configuration is very simple. Need to note is that I first in the ApplicationContext configuration Freemarkerconfigurer This class, many articles are not configured it, this class is not configured will also error, And this class must be prioritized before freemarkerviewresolver, about Freemarkerviewresolver will be described below.

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" 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 ">
<!--configured here is freemarker--> <bean id= "Freemarkerconfig" class= "Org.springframework.web.servlet.view.free Marker. Freemarkerconfigurer "> <property name=" Templateloaderpath "value="/web-inf/views/"/> <property Name= "Freemarkersettings" > <props> <prop key= "Template_update_delay" &GT;0&LT;/PROP&G T <prop key= "default_encoding" >UTF-8</prop> <prop key= "Number_format" >0.##########</prop > <prop key= "Datetime_format" >yyyy-mm-dd hh:mm:ss</prop> <prop key= "Classi C_compatible ">true</prop> <prop key=" Template_exception_handler ">ignore</prop> </props> </property> </bean></beans>

And then the Spring-servlet.xml. Let's go straight to the notes, I've written in the notes in detail.

<?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:context= "Http://www.springframework.org/schema/context"       xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:jdbc= "Http://www.springframework.org/schema/jdbc" xmlns:task= "Http://www.springframework.org/schema/task" xmlns:cache= "http://www.springframework.org/schema/ Cache "xmlns:p=" http://www.springframework.org/schema/p "xsi:schemalocation=" http://www.springframework.org/sc HEMA/JDBC http://www.springframework.org/schema/jdbc/spring-jdbc.xsd Http://www.springframework.org/schema/task Http://www.springframework.org/schema/task/spring-task.xsd Http://www.springframework.org/schema/beans/HTTP Www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/cache/HTTP Www.springframework.org/schema/cache/spring-cache.xsd HTTP://WWW.SPRIngframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org /schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--<context: Annotation-config/>--> <!--converts a class marked @controller annotation to bean--<context:component-scan base-package= "con Troller "/> <!--start the Spring MVC Annotation feature, complete the mapping of requests and annotations Pojo-<bean class=" ORG.SPRINGFRAMEWORK.WEB.SERVLET.MVC.A Nnotation.          Annotationmethodhandleradapter "/> <!--setting Freemarker profile path--<bean id=" Freemarkerconfiguration " class= "Org.springframework.beans.factory.config.PropertiesFactoryBean" > <!--commented out the code below is the configuration that guides Freemarker's basic information Location, because I have moved the configuration information to the ApplicationContext file, so there is no need to exist here, no comment and no problem--<!--<property name= "Location" value= "CLA Sspath:/web-inf/config/freemarker.properties "/>--> </bean> <bean id=" Viewresolver "class=" Org.sprin Gframework.web.servlet.view.freemArker. Freemarkerviewresolver "> <property name=" exposerequestattributes "value=" true "/> <property name = "Exposesessionattributes" value= "true"/> <property name= "Viewclass" > &LT;VALUE&GT;ORG.SPRINGF ramework.web.servlet.view.freemarker.freemarkerview</value> </property> <property name= "cache "><value>true</value></property> <!--here, I'm going to comment on a line of code like this, This line of code is meant to guide the location of the files that Freemarker needs to parse. Comment out the reason is because there is such a line of code in Applicationcontext.xml: <property name= "Templateloaderpath" value= "/web-inf/views/"/> has been specified The view location. If we still keep the code below, the page returns 406 error-<!--<property name= "prefix" ><value>/web-inf/views/</value& gt;</property>--> <property name= "suffix" ><value>.html</value></property> & Lt;property name= "ContentType" > <value>text/html; Charset=utf-8</value> </property> &LT;/BEAN&GT <!--here is optional--<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMu Ltipartresolver "p:defaultencoding=" Utf-8 "/></beans>

  

Here is my MAVEN configuration information, which you can select as needed. But basically keep it.

<dependencies> <!--spring4 begin--> <dependency> <groupid>org.springfram Ework</groupid> <artifactId>spring-context</artifactId> <version>4.2.0.build -snapshot</version> </dependency> <dependency> <groupid>org.springframewo Rk</groupid> <artifactId>spring-webmvc</artifactId> <version>4.2.0.build-sna pshot</version> <!--<version>${org.springframework-version}</version>--> </de pendency> <dependency> <groupId>org.springframework</groupId> <artifa Ctid>spring-web</artifactid> <version>4.2.0.BUILD-SNAPSHOT</version> </dependenc y> <dependency> <groupId>org.springframework</groupId> <artifactid&gt ; SPRING-TEST&LT;/ARTIFACtid> <version>4.2.0.BUILD-SNAPSHOT</version> </dependency> <dependency&gt            ; <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> & lt;version>4.2.0.build-snapshot</version> </dependency> <dependency> <gro Upid>org.springframework</groupid> <artifactId>spring-core</artifactId> <ver sion>4.2.0.build-snapshot</version> </dependency> <dependency> &LT;GROUPID&G T;org.springframework</groupid> <artifactId>spring-oxm</artifactId> <version&gt ;4.2.0.build-snapshot</version> </dependency> <dependency> <groupid>org.s Pringframework</groupid> <artifactId>spring-tx</artifactId> <version>4.2.0.b Uild-snapshot</version> </dependency> <dependency> <groupid>org.springframework</ Groupid> <artifactId>spring-jdbc</artifactId> <version>4.2.0.BUILD-SNAPSHOT< /version> </dependency> <!--spring end--> <dependency> &LT;GROUPID&G T;junit</groupid> <artifactId>junit</artifactId> <version>3.8.1</version&            Gt            <scope>test</scope> </dependency> <!--log4j---<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1 .2.17</version> </dependency> <!--servlet API--<dependency> &L T;groupid>javax.servlet</groupid> <artifactId>javax.servlet-api</artifactId> &L T;version>3.0.1</version> <scope>provided</scope> </dependency> <!--start a Pache-<dependency> <groupId>commons-logging</groupId> <artifactid >commons-logging</artifactId> <version>1.1.3</version> </dependency> & Lt;dependency> <groupId>commons-collections</groupId> <artifactid>commons-colle ctions</artifactid> <version>3.2.1</version> </dependency> <dependency > <groupId>commons-fileupload</groupId> <artifactid>commons-fileupload</arti        Factid> <version>1.3</version> </dependency> <!--end Apache-- <dependency> <groupId>org.freemarker</groupId> <artifactid>freemarker</          Artifactid>  <version>2.3.22</version> </dependency> <dependency> <groupid>org .springframework</groupid> <artifactId>spring-context-support</artifactId> <ver Sion>4.1.6.release</version> </dependency>

We need to pay attention to this section of MAVEN information, before I was not configured, Results Greemarker has been reported org.springframework.ui.freemarker.FreeMarkerConfigurationFactory this class can not find, because freemarkerconfigurationfactor Y this class is actually in the package below. Feel the eggs.

    
<dependency>            <groupId>org.springframework</groupId>            <artifactId> spring-context-support</artifactid>            <version>4.1.6.RELEASE</version>        </dependency >

Controller: Only one step to jump work.

Package Controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.responsebody;import Org.springframework.web.bind.annotation.restcontroller;import org.springframework.web.servlet.modelandview;/** * Created by Porco on 2015/5/21 0021. */@RestControllerpublic class Logincontroller {    @RequestMapping ("/login") Public    Modelandview login () {        return new Modelandview ("Login");}    }

  

HTML: Haha, Porco is my nickname, in fact, this is not an English, is an Italian bar, Dick Silk outfit forced

<! DOCTYPE html>

Final startup Project, running result:

The spring and Freemarker configuration Although many twists and turns, but also learned a lot of the meaning of configuration information, I have a university classmate said, in the study of the energy and financial resources are worthwhile.

spring4.2 full Web project (using HTML view parser)

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.