Build a spring MVC project with Eclipse (plot)

Source: Internet
Author: User
Tags naming convention

Recent work requires a quick understanding of the Spring MVC architecture, so there are a lot of tutorials on the web. Seriously, Baidu came out of the basic are the same, but I encountered a variety of bizarre problems. Of course, after finishing looking back, found that those problems are not problems, haha ~ Main still do not understand Java these things, encountered problems will find it difficult, all kinds of check, but the results of Baidu is very helpless, different URLs, the same content!

I refer to a blog to build the project, the URL is: http://limingnihao.iteye.com/blog/830409. According to the process of this blog to create a project, but because of the actual situation, there are some different in the middle.

Below I have used the relevant software version of the list, although I do not know what the relationship, but, it is a list, hahaha ~ ~


Because I don't know what the column is, I'm just going to take a picture of all this, just know to use Jdk,maven,tomcat, and the Java compiler (like 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 is not what I want, need to be modified, right-click it, select "Build Path" ==> "ConfigureBuild Path")


Figure 6


Figure 7 (I don't know why I chose this, but I saw my colleague choose this before.) )


Figure 8



Figure 9


By this step, a Mave project has been built. Let's configure some things now.

2, Configuration pom.xml (can be added directly to pom.xml inside, configure some 7788 things. JUnit, Jstl, Spring pack )

<dependencies> <!--JUnit--><dependency><groupid>junit</groupid><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--><dependency><groupid>org.springframework</groupid>< Artifactid>spring-core</artifactid><version>4.1.2.release</version></dependency> <dependency><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><dependency><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>< Artifactid>spring-jdbc</artifactid><version>4.1.2.release</version></dependency> <dependency><groupid>org.springframework</groupid><artifactid>spring-webmvc</ Artifactid><version>4.1.2.release</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-aspects</artifactid><version>4.1.2. Release</version></dependency><dependency><groupid>org.springframework</groupid> <artifactid>spring-test</artifactid><version>4.1.2.release</version></dependency ><!--Spring's basic dependency end--</dependencies>

3, add a folder under WebApp "Web-inf", under the folder to add a file "Web. Xml" (the contents below can be copied all the past)

<?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 "><!--project name, prevent default names--<context-param> <param -name>webapprootkey</param-name> <param-value>maven.example.root</param-value> </con text-param> <!--Spring log4j listener--<listener> <listener-class>org.springframew Ork.web.util.log4jconfiglistener</listener-class> </listener> <!--character Set filter--<fil Ter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springfra Mework.web.filter.characterencodingfilter</filter-class> <init-param> <param-name> Encoding</param-name> <p aram-value>utf-8</param-value> </init-param> <init-param> <param- Name>forceencoding</param-name> <param-value>true</param-value> </init-param > </filter> <filter-mapping> <filter-name>characterencodingfilter</filter-name&          Gt <url-pattern>/*</url-pattern> </filter-mapping> <!--Spring View Distributor-&LT;SERVL et> <servlet-name><span style= "color: #ff6666;" >dispatcher0121</span></servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param -name>contextconfiglocation</param-name> <param-value>/web-inf/<span style= "color: #ff6666;" >dispatcher0121</span>-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name ><span style= "color: #ff6666;" >dispatcher0121</span></servlet-name> <url-pattern>*.do</url-pattern> </servle T-mapping> </web-app>
There are several noteworthy 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" This 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 (can be copied 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/context"          Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:tx= "Http://www.springframework.org/schema/tx" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://www.springframework.org/s CHEMA/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.springframewo Rk.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www. SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www . Springframework.org/schEma/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <mvc:annotation-driven/> <context:component-scan base-package= "<span style=" color: #ff6666; " >example0121</span> "/> <bean class="  Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name=" prefix "value=" <span style= "COLOR: #ff6666;" >/WEB-INF/views/</span> "/> <property name=" suffix "value=". jsp "/> </bean> </   Beans>

In the Dispatcher0121-servlet.xml file there are also places to note, "example0121" This is the next package, the name can be arbitrary, but not all the numbers. I don't want to say, I just lazy use the numbers, resulting in the previous project has been a problem.

Figure 10

5 . Create a controller layer test class (This is something, I do not know.) Only know how to operate)



Figure 11

According to this operation, you can add a package or add a class, first add a class, if you find that the package does not write "example0121", then Cancel, and then add a package, the name of the package is "example0121". Then create a class called "Generalcontroller".

Figure 12

Then write the code well, 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") 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 the Red Fork in your project and make a mistake, 13. This time you need to calm down, two ways. One is to ignore it, and the other is to delete the mistake. I chose to ignore it, I because of this error check for a long time information, inexplicable irritability, tried the method of online said, or not. Finally asked colleagues, colleagues said, you can run a bit can be used. I ran the project and found that it could be used. Then the colleague said, "ignore it." Seems to be the problem with the checksum, it doesn't affect anyway. One or two?!


Figure 13

7. Operation

Figure 14


Figure 15


Figure 16


Figure 17


Figure 18


Now you've created the project. To start a deep discussion.

Build a spring MVC project with Eclipse (plot)

Related Article

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.