Original intention: I beginner springmvc encounter all kinds of strange problems, online various technical forum posts and uneven, difficult to one step to achieve the configuration of the effect, here I will be the summary of my configuration to write here for everyone to learn SPRINGMVC colleagues together to study the use!
About SPRINGMVC I'm not going to go into this, and presumably everyone who has configured spring has more or less learned the basics of some springmvc, maybe some people use myeclipse or eclipse to build a project, I will tell you that the steps here are very similar, in myeclipse you need to build a Web project, then add Spring support, follow the jar package yourself to download the import, other configurations and this article indistinguishable. But I recommend that you use idea, this compiler is recognized as the best Java compiler, presumably writing code will be more comfortable and fast!
In the help of many friends, this article constantly improve, here will revise the complete version of the re-published again, look at the vast number of netizens elegance!!!
Here's how to build a Maven Springmvc project under IntelliJ idea:
First, there is a need to have a IntelliJ idea can go to the official website to download, although it is charged, but not too expensive.
Second, run the compiler to create a new project
Select Maven in the pop-up form and tick the MAVEN template you want to build--Choose WebApp here
Then fill in the corresponding MAVEN project group information (this is more casual)
Fill in your Maven local repository path (this doesn't have to explain too much, using Maven's foundation);
Fill in the project name and the project build path, and check that the fill is correct below:
If correct, click finish->
MAVEN will automatically create some of the configuration information and directory structure needed, during which time we can find the required jar packages and configure them in the MAVEN configuration file Pom.xml, as shown in the following steps:
Here if you do not know what to use the jar package will go to Baidu Springmvc needed jar package, Then find the jar package separately in MAVEN's official link to configure the Pom.xml, as follows (here I demonstrate to the MAVEN official website to find the XML configuration section, the specific need for the jar package to go to the pom.xml I showed later):
Login http://mvnrepository.com/ sample Find Spring-beans
Click Find Results
Here you can see the latest version as well as the most used version, your choice--the configuration file inside the jar package version is best to choose the same version to avoid version conflicts;
We choose the first to enter the page, you can see MAVEN configuration pom.xml file, click the code directly copy (automatic copy);
Then copy the copied code to the Pom.xml file, maven will automatically download the required jar package, we do not need to ignore (if there is a network is not smooth situation, please find a way to download the jar package or contact me, and then copy the downloaded jar package to the corresponding Maven local warehouse folder can also);
Here I show that my provisioned maven pom.xml,maven will automatically download the required jar packages to the MAVEN repository.
Pom.xml
Once the system is built, we can see the directory structure
Incomplete can be in accordance with the requirements to complete the file structure, MAVEN project file structure is divided into sources,tests,resources,test resources,excluded, we need to distinguish between the different types of folders:
The Configuration folder type method is as follows:
You can select a folder by clicking on the button above to label the folder type!
Third, configuration Springmvc
After everything is ready, we continue to configure SPRINGMVC specific information:
First you need to configure Web. XML This needless to say, site project run the first load is Web. XML, into
Src->main->webapp->web-inf->web.xml
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 3 Xmlns= "Http://java.sun.com/xml/ns/javaee" 4 xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee Http://jav A.sun.com/xml/ns/javaee/web-app_3_0.xsd "5 version=" 3.0 "> 6 <welcome-file-list> 7 <welcom E-file>/index.jsp</welcome-file> 8 </welcome-file-list> 9 <!--Spring MVC configuration-->10 <ser Vlet>11 <servlet-name>spring</servlet-name>12 <servlet-class>org.springframework.web. Servlet. Dispatcherservlet</servlet-class>13 <!--You can customize the location and name of the Servlet.xml configuration file by default to the Web-inf directory with the name [<servlet-nam E>]-servlet.xml, such as Spring-servlet.xml15 <init-param>16 <param-name>contextconfiglocation& Lt;/param-name>17 <param-value>/web-inf/spring-servlet.xml</param-value>18 </init-pa Ram>-->19 20 <!--The Load-on-startup element flags whether the container loads the servlet at startup (instantiates and invokes its init () method)-->21 <load-on-startup>1< /load-on-startup>22 </servlet>23 <servlet-mapping>25 <servlet-name>spring</serv Let-name>26 <url-pattern>/</url-pattern>27 <!--<URL-PATTERN>*.DO</URL-PATTERN&G T;-->28 </servlet-mapping>29 <!--spring configuration-->31 <listener>32 <listener-cla Ss>org.springframework.web.context.contextloaderlistener</listener-class>33 </listener>34 <! --Specifies the directory where the spring bean's configuration file resides. The default configuration is in the Web-inf directory-->36 <context-param>37 <param-name>contextconfiglocation</param-name>38 <param-value>classpath:applicationcontext.xml</param-value>39 </context-param>40 </web -app>
Then create a new Spring-servlet.xml under the Web-inf folder
Configuring the Spring-servlet.xml here is the way to use annotations:
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <beans xmlns= "Http://www.springframework.org/schema/beans" 3 x Mlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context= "http://www.springframework.org/schema/ Context "5 xmlns:mvc=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC "6 xsi:schemalocation=" Http://www.springfram Ework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8/HTTP Www.springframework.org/schema/context 9 HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/CONTEXT/SPRING-CONTEXT-3.1.XSD10 Http://www.springframework.org/schema/mvc11 http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd ">12 <!--start the note-driven Spring MVC feature, registration request URL and annotation Pojo class method mapping--<mvc:annotation-driven >15 < ;/mvc:annotation-driven>17 <!--start the package scan feature to register with @controller, @service, @repository, @ component classes such as annotations become spring's bean-->19 <context:component-scan base-package= "Hellospringmvc.conTroller "/>20 <!--resolution of the Model view name, add the prefix-->21 <bean class=" on request with the Model view name Org.springframework.web.servlet.view . Internalresourceviewresolver ">22 <property name=" Viewclass "value=" org.springframework.web.servlet.view.Jst LView "/>23 <property name=" prefix "value="/"/> <!--prefix-->24 <property name=" suffix "Value=". jsp "/> <!--suffix-->25 </bean>26 <!--access to static files (jpg,js,css)-->27 <mvc:res Ources location= "/files/" mapping= "/files/**"/>28 <mvc:resources location= "/scripts/" mapping= "/scripts/**"/& gt;29 <mvc:resources location= "/styles/" mapping= "/styles/**"/>30 <mvc:resources location= "/Views/" Mapp ing= "/views/**"/>31 </beans>
Here we configure the static file path, so in the directory structure to create a few new folders to put static files such as scripts, picture files, views and so on.
Then create a new applicationcontext.xml under the Resources Resource folder
Configuration Applicationcontext.xml There is no configuration to configure what information, because I use the MySQL JDBC Connection database, no use of Hibernate and other Orm, here can put hibernate related configuration items inside.
1 <?xml version= "1.0" encoding= "UTF-8"? >2 <beans3 xmlns= "Http://www.springframework.org/schema/beans" 4 xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemalocation= "/http Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd ">6 <!--We can add the beans we need to configure, add the appropriate database connection and transaction processing, and so on, to facilitate further expansion 7 -->8 </beans>
Here we set up the basic XML configuration file for Springmvc, then we perform a small instance to test the configured SPRINGMVC.
Four: Test SPRINGMVC configuration:
1, under the Controller folder to create a new class name called TestController, the code is as follows:
Package Hellospringmvc.controller;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.responsebody;/** * Created by Administrator on 2016/8/29. */@Controller @requestmapping (value = "/test/*")//access URL address prefix, can not write, write must precede the method URL to precede the class URL to distinguish the Controller public class testcontroller{ //Access address: http://localhost:8080/Test/returnSuccess @RequestMapping (value = "returnsuccess") //Actual access URL address public String returnsuccess () { return "/views/success"; Return to the Success.jsp page under the Views folder } //Access address: http://localhost:8080/Test/returnString @RequestMapping (value = "Returnstring", produces = {"Text/plain;charset=utf-8"}) //produces used to solve the problem of returning Chinese garbled characters, Application/json; solving Chinese garbled characters for JSON @ResponseBody //is used to return a string, not write returns the view public string returnstring () { return ' Hello return string this is Chinese, And there is no garbled "; }}
Then we build a success.jsp under the Views folder to write anything:
And then try to run, idea needs to be configured to run the environment, configured as follows
In the pop-up tab, select the "+" sign to add the operating environment!
I choose the Tomcat startup Project here:
Then in the Popup tab, select Fix
Then the application is available, and of course there are some optional configurations as follows (this is my configuration):
Then you can run it!!!
OK, click to Run, Google automatically pop up the page!
After running, enter in the browser address bar: http://localhost:8080/Test/returnString
You can see that we run the second URL address to successfully return a string, and there is no Chinese garbled!
Test the first URL address below: http://localhost:8080/Test/returnSuccess
can see that the successful return to success.jsp! Note that our SPRINGMVC has no configuration errors.
Attach the project directory structure again
Others are accustomed to using JSON, where the JSON can be directly when the string output, the foreground can be obtained, the JSON jar package configuration can also be self-query, here no longer repeat.
2016-08-29 19:27:41
This article is seven small station main original works, reprint please indicate source: http://www.cnblogs.com/qixiaoyizhan/
Project source code Download:
Http://files.cnblogs.com/files/qixiaoyizhan/AHelloSpringMvcDemo.zip
In addition Anlibo Master personal blog, welcome to visit, another site graced!!!
Http://qixiao.me
Thx and Best regards~
----------above is the content of the reprint--------
Here are the notes after the personal practice:
Since Maven will automatically download dependent packages for dependencies, Spring's dependency package in Pom.xml only needs to rely on SPRING-WEBMVC. My whole pom.xml is dependent on two packages, one is Spirng-webmvc, one is Jstl (this package is used to parse JSP pages). For more information on how to view the graphs between dependent packages, refer to the http://www.cnblogs.com/luohengstudy/articles/7904874.html
Go Springmvc Framework starter configuration idea to build a MAVEN project