<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); > </span><span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255);" > As a new programmer, these two days received instructions from the boss, first to get Spring MVC implementation restful interface, familiar with the previous technology. This time the head is so big, Java forgot almost, let alone use spring to build MVC. No way can only use the Niang to help, but on the internet all kinds of ideas, all kinds of confused, finally found a more suitable for novice Bowen, and then thanked. </span>
Reproduced below:
Let me make a list of the relevant software versions I used, although I don't know what it's all about, but just list
Because I don't know what the columns are, I cut them all up, only to use Jdk,maven,tomcat, and the Java compiler (such as Eclipse)
Start building Projects Now
1. Create a MAVEN project
Figure 1
Figure 2
Figure 3 (The first check box must be box, anyway I do not choose is wrong ~)
Figure 4 (Packaging Select War. Anyway, I had a problem with the jar before I chose it.
Figure 5 (The red box doesn't show what I want, I need to modify it, right click it, choose "Build Path" ==> "Configurebuild Path")
Figure 6
Figure 7 (I don't know why I chose this, but I just saw my colleague choose this one before.) )
Figure 8
Figure 9
By this step, a Mave project has been built. We're going to configure something now.
2, configure Pom.xml (can be directly added to the pom.xml inside, configure some 7788 of things. JUnit, Jstl, spring packages)
<dependencies> <!--junit--> <dependency> <groupid>junit</gr Oupid> <artifactId>junit</artifactId> <version>4.12-beta-3</version>
;
<scope>test</scope> </dependency> <!--log4j--> <dependency>
<groupId>log4j</groupId> <artifactId>log4j</artifactId>
<version>1.2.17</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1 .2</version> </dependency> <!--spring's basic dependencies start--> <depe Ndency> <groupId>org.springframework</groupId> <artifactid>spring-core< ;/artifactid> <version>4.1.2.RELEASE</version> </dependency> <depende Ncy> <groupId>org.springframework</groupId> <artifactid>spring-expression
</artifactId> <version>4.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring
-beans</artifactid> <version>4.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>
Spring-aop</artifactid> <version>4.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid >spring-context</artifactid> <version>4.1.2.RELEASE</version> </dependency> <depen Dency> <groupId>org.springframework</groupId> <artifactid>spring-context-
Support</artifactid> <version>4.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>
;spring-tx</artifactid> <version>4.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid
>spring-web</artifactId> <version>4.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifacti D>spring-jdbc</artifactid> <version>4.1.2.RELEASE</version> </dependency> <depe Ndency> <groupId>org.springframework</groupId> <artifactid>spring-webmvc& Lt;/artifactid> <version>4.1.2.RELEASE</version> </dependency> < Dependency> <groupId>org.springframework</groupId> <artifactid>spring-asp
Ects</artifactid> <version>4.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <ARTIFACTID>SP
Ring-test</artifactid> <version>4.1.2.RELEASE</version> </dependency> <!--spring's basic dependency end--> </dependencies>
3, under the WebApp to add a folder "Web-inf", under the folder to add a file "Web.xml" (the following content can be copied all over)
<?xml version= "1.0" encoding= "UTF-8"?> <web-app 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 "version=" 2.5 > <!--differentiate project names against default duplicate--> <context-param> <param-name>webAppRootKey</param-name> <param-value>maven.example.root</param-
Value> </context-param> <!--spring log4j listener--> <listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!--character Set filter--> <filter> <filter-name>characterencodingfilter</filt
Er-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> <init-param> <PARAM-NAME>FORCEENCODING</PARAM-NAME&G
T <param-value>true</param-value> </init-param> </filter> <filter-map Ping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</
Url-pattern> </filter-mapping> <!--Spring View distributor--> <servlet> <servlet-name>dispatcher0121</servlet-name> <servlet-class>org.springframework.web.serv Let. Dispatcherservlet</servlet-class> <init-param> <param-name>contextconfigloca
Tion</param-name> <param-value>/WEB-INF/dispatcher0121-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> < Servlet-mapping> <servlet-name>dispatcher0121</servlet-name> <URL-PATTERN>*.D O</url-pattern> </servlet-mapping> </web-app>
There are a few notable places in the Web.xml file where the red "dispatcher0121" can be changed to the string you want, but the three places must be the same. [servlet-name]+ "-servlet.xml" is a naming convention for the file name of the spring configuration file.
4, under "webapp/web-inf/" Add a File "Dispatcher0121-servlet.xml", this file is said above the things (you can copy all to the new file)
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:context= "Http://www.springframework.org/schema/co ntext "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xmlns:tx=" HTTP://WWW.SPRINGFRAMEWORK.O Rg/schema/tx "xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://www. SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 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-c Ontext-3.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/m Vc/spring-mvc-3.0.xsd
Http://www.springframework.org/schema/tx Http://www.springframework.org/schema/tx/spring-tx-3.0.xs
D "> <mvc:annotation-driven/> <context:component-scan base-package=" example0121 "/> <bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property Name= "prefix" value= "/web-inf/views/"/> <property name= "suffix" value= ". jsp"/> </BEAN&G
T </beans>
Dispatcher0121-servlet.xml file also have to pay attention to the place, "example0121" This is the next package, the name can be arbitrary, but not all of the numbers. I do not want to say that I was lazy with numbers, leading to the project has been built before the problem.
Figure Ten
5, create a controller layer test class (This is what thing, I do not know.) Only know how to operate)
Figure
According to this operation, you can add a package or add a class, add a class, if found in the package there is no "example0121", then Cancel, and then add a package, the name of the package is "example0121". Then create a class called "Generalcontroller".
Figure
Then write the code like the following
Package example0121;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.Model;
Import org.springframework.web.bind.annotation.RequestMapping;
@Controller public
class Generalcontroller {
@RequestMapping (value= "index.do") is public
void index_jsp ( Model model) {
Model.addattribute ("str0121", "Hellow World");
System.out.println ("index.jsp");
}
6, write JSP page. Add a folder "views" under "webapp/web-inf/" and add a File "index.jsp". This path is set down in the "Dispatcher0121-servlet.xml".
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <%@ taglib prefix="
C "uri=" Http://java.sun.com/jsp/jstl/core "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
At this point you can see that your project appears with a red fork and made a mistake, as shown in Figure 13. This time you have to calm down, two ways. One is to ignore it, and the second is to delete this error. I chose to ignore it, I because this mistake for a long time to check the information, inexplicable irritability, tried to speak on the Internet method, or not. Finally asked a colleague, colleagues said, you can run it. I ran the project and found it could be used. Then the colleague says, ignore it. It seems to be the problem of checking, anyway it doesn't affect. One or two..
Figure
7, Operation
Figure
Figure
Picture
Figure
Figure
The above blog address is: http://blog.csdn.net/fulai0_0/article/details/42967481
——————-———————————— Split Line ————————————————————————————————————————
This is basically the end of the project, the only problem I've encountered is that the project will not start during startup.
finally asked a friend, modify the method to add a sentence in the Pom.xml file:
<packaging>war</packaging>
The location is as follows:
In general, it is not easy to build a simple MVC framework for a friend who is not familiar with spring, to make a day to understand it, and finally leave a few more useful posts for your reference.
Http://www.cnblogs.com/davidwang456/p/4122842.html
http://blog.csdn.net/dddddz/article/details/8777126
http://www.oschina.net/question/857812_220501