Build spring mvc project with Eclipse (illustration)

Keywords: Attribute Spring xml Java

Recent work requires quick understanding spring mvcFramework So I found a lot of related courses on the internet. To be honest, Baidu basically comes out the same, but I have encountered all kinds of strange problems. Of course, when I look back, I find that those problems are not problems. Ha ha ha ha ~I still don't understand them. Java These things, when encountering problems, will feel difficult, various checks, but Baidu's results are very helpless, different websites, the same content!

I referred to a blog to build the project. The website is http://limingnihao.iteye.com/blog/830409. The project was created according to the process of this blog, but there are still some differences between them due to the actual situation.

Next, I will list the relevant software versions that I use. Although I don't know what the relationship is, I'd like to list them.Ha ha ha ~ ~


Because I don't know which ones to list, I cut them all up.Only know to use jdk, maven, tomcat, and java compilers (such as eclipse)

Start building projects now

1. Create a maven project

 

Figure 1


Figure 2


Figure 3 (The first check box must be a box, anyway I'm wrong if I don't choose it here ~)



Figure 4 (Packaging selects war). Anyway, I had a problem choosing jar before.




Figure 5 (The red box does not show what I want, need to be modified, right-click it, select "Build Path"=> "Configure Build Path")


Figure 6


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


Figure 8



Figure 9


At this point, a mave project has been built. Now let's configure something.

2. Configure pom.xml (you can add it directly to pom.xml and configure something seven or eight. junit, jstl, spring package)

  1. <dependencies>  
  2.         <!-- junit -->  
  3.         <dependency>  
  4.             <groupId>junit</groupId>  
  5.             <artifactId>junit</artifactId>  
  6.             <version>4.12-beta-3</version>  
  7.             <scope>test</scope>  
  8.         </dependency>  
  9.         <!-- log4j -->  
  10.         <dependency>  
  11.             <groupId>log4j</groupId>  
  12.             <artifactId>log4j</artifactId>  
  13.             <version>1.2.17</version>  
  14.         </dependency>  
  15.           
  16.         <dependency>  
  17.             <groupId>jstl</groupId>  
  18.             <artifactId>jstl</artifactId>  
  19.             <version>1.2</version>  
  20.         </dependency>  
  21.           
  22.         <!-- spring Basic dependence start -->  
  23.   
  24.         <dependency>  
  25.             <groupId>org.springframework</groupId>  
  26.             <artifactId>spring-core</artifactId>  
  27.             <version>4.1.2.RELEASE</version>  
  28.         </dependency>  
  29.   
  30.         <dependency>  
  31.             <groupId>org.springframework</groupId>  
  32.             <artifactId>spring-expression</artifactId>  
  33.             <version>4.1.2.RELEASE</version>  
  34.         </dependency>  
  35.   
  36.         <dependency>  
  37.             <groupId>org.springframework</groupId>  
  38.             <artifactId>spring-beans</artifactId>  
  39.             <version>4.1.2.RELEASE</version>  
  40.         </dependency>  
  41.   
  42.         <dependency>  
  43.             <groupId>org.springframework</groupId>  
  44.             <artifactId>spring-aop</artifactId>  
  45.             <version>4.1.2.RELEASE</version>  
  46.         </dependency>  
  47.   
  48.         <dependency>  
  49.             <groupId>org.springframework</groupId>  
  50.             <artifactId>spring-context</artifactId>  
  51.             <version>4.1.2.RELEASE</version>  
  52.         </dependency>  
  53.         <dependency>  
  54.             <groupId>org.springframework</groupId>  
  55.             <artifactId>spring-context-support</artifactId>  
  56.             <version>4.1.2.RELEASE</version>  
  57.         </dependency>  
  58.   
  59.         <dependency>  
  60.             <groupId>org.springframework</groupId>  
  61.             <artifactId>spring-tx</artifactId>  
  62.             <version>4.1.2.RELEASE</version>  
  63.         </dependency>  
  64.   
  65.         <dependency>  
  66.             <groupId>org.springframework</groupId>  
  67.             <artifactId>spring-web</artifactId>  
  68.             <version>4.1.2.RELEASE</version>  
  69.         </dependency>  
  70.         <dependency>  
  71.             <groupId>org.springframework</groupId>  
  72.             <artifactId>spring-jdbc</artifactId>  
  73.             <version>4.1.2.RELEASE</version>  
  74.         </dependency>  
  75.   
  76.         <dependency>  
  77.             <groupId>org.springframework</groupId>  
  78.             <artifactId>spring-webmvc</artifactId>  
  79.             <version>4.1.2.RELEASE</version>  
  80.         </dependency>  
  81.         <dependency>  
  82.             <groupId>org.springframework</groupId>  
  83.             <artifactId>spring-aspects</artifactId>  
  84.             <version>4.1.2.RELEASE</version>  
  85.         </dependency>  
  86.   
  87.         <dependency>  
  88.             <groupId>org.springframework</groupId>  
  89.             <artifactId>spring-test</artifactId>  
  90.             <version>4.1.2.RELEASE</version>  
  91.         </dependency>  
  92.         <!-- spring Basic dependence End -->  
  93.   </dependencies>  

3. Add a folder "WEB-INF" under webapp and a file "web.xml" under the folder (the following can be copied all over)

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     version="2.5">  
  5.     <!-- Distinguish project names to prevent default renaming -->    
  6.     <context-param>    
  7.         <param-name>webAppRootKey</param-name>    
  8.         <param-value>maven.example.root</param-value>    
  9.     </context-param>    
  10.     
  11.     <!-- Spring Of log4j Monitor -->    
  12.     <listener>    
  13.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>    
  14.     </listener>    
  15.     
  16.     <!-- character set Filter  -->    
  17.     <filter>    
  18.         <filter-name>CharacterEncodingFilter</filter-name>    
  19.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    
  20.         <init-param>    
  21.             <param-name>encoding</param-name>    
  22.             <param-value>UTF-8</param-value>    
  23.         </init-param>    
  24.         <init-param>    
  25.             <param-name>forceEncoding</param-name>    
  26.             <param-value>true</param-value>    
  27.         </init-param>    
  28.     </filter>    
  29.     <filter-mapping>    
  30.         <filter-name>CharacterEncodingFilter</filter-name>    
  31.         <url-pattern>/*</url-pattern>    
  32.     </filter-mapping>    
  33.     
  34.     <!-- Spring view Dispenser -->    
  35.     <servlet>    
  36.         <servlet-name>dispatcher0121</servlet-name>    
  37.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
  38.         <init-param>    
  39.             <param-name>contextConfigLocation</param-name>    
  40.             <param-value>/WEB-INF/dispatcher0121-servlet.xml</param-value>    
  41.         </init-param>    
  42.         <load-on-startup>1</load-on-startup>    
  43.     </servlet>    
  44.     <servlet-mapping>    
  45.         <servlet-name>dispatcher0121</servlet-name>    
  46.         <url-pattern>*.do</url-pattern>    
  47.     </servlet-mapping>    
  48.   
  49. </web-app>  
There are several noteworthy points in the web.xml file. The red "dispatcher 0121" above can be changed to the string you want, but the three places must be the same. [servlet-name]+"-servlet.xml" is the naming rule for file names of spring configuration files.

4. Add a file "dispatcher0121-servlet.xml" under "webapp/WEB-INF/". This file is just what I said above (all can be copied to a new file)

  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"     
  3.        xmlns:aop="http://www.springframework.org/schema/aop"     
  4.        xmlns:context="http://www.springframework.org/schema/context"    
  5.        xmlns:mvc="http://www.springframework.org/schema/mvc"     
  6.        xmlns:tx="http://www.springframework.org/schema/tx"     
  7.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  8.        xsi:schemaLocation="http://www.springframework.org/schema/aop     
  9.         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd     
  10.         http://www.springframework.org/schema/beans     
  11.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
  12.         http://www.springframework.org/schema/context     
  13.         http://www.springframework.org/schema/context/spring-context-3.0.xsd     
  14.         http://www.springframework.org/schema/mvc     
  15.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd     
  16.         http://www.springframework.org/schema/tx     
  17.         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">    
  18.     
  19.     <mvc:annotation-driven />    
  20.     <context:component-scan base-package="example0121" />    
  21.     
  22.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
  23.         <property name="prefix" value="/WEB-INF/views/" />    
  24.         <property name="suffix" value=".jsp" />    
  25.     </bean>    
  26.     
  27. </beans>    

The dispatcher0121-servlet.xml file also has something to note, "example0121" is the next package, the name can be arbitrary, but not all the numbers. I don't want to say that I'm just lazy with numbers, causing problems with previous projects.

Figure 10

5. Create a Controller layer test I don't know what this is. Only know how to operate)



Figure 11

According to this operation, you can add packages or classes. First, you can add classes. If you find that there is no "example0121" in the package, you can cancel it. Then you can add a package. The name of the package is "example0121". Then create a class called General Controller.

Figure 12

Then write the code as follows

  1. package example0121;  
  2.   
  3. import org.springframework.stereotype.Controller;    
  4. import org.springframework.ui.Model;    
  5. import org.springframework.web.bind.annotation.RequestMapping;    
  6.     
  7. @Controller    
  8. public class GeneralController {    
  9.     
  10.     @RequestMapping(value="index.do")    
  11.     public void index_jsp(Model model){    
  12.         model.addAttribute("str0121", "Hellow world");    
  13.         System.out.println("index.jsp");    
  14.     }    
  15. }    

6. Write JSP pages. Add a folder "views" under "webapp/WEB-INF/" and a file "index.jsp". This path was determined in "dispatcher0121-servlet.xml".

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>    
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>    
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">    
  4. <html>    
  5.     <head>    
  6.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
  7.         <title>Insert title here</title>    
  8.     </head>    
  9.         
  10.     <body>    
  11.         <c:out value="${str0121}"></c:out>    
  12.     </body>    
  13. </html>    
At this point, you can see that your project has a red fork and a mistake has been reported, as shown in Figure 13. You have to be calm at this time. There are two ways. One is to ignore it, the other is to delete it. I chose to ignore it, because I checked the wrong information for a long time, inexplicably irritable, tried the online method, or not. Finally, I asked my colleagues, and they said, can you run it? I ran the project and found it working. Then my colleague said, ignore it. It seems like a question of verification, but it doesn't matter now. one or two?!


Figure 13

7, operation

Figure 14


Figure 15


Figure 16


Figure 17


Figure 18


Now it's time to create a project. We need to start a deep discussion.

Posted by kevisazombie on Fri, 22 Mar 2019 07:18:56 -0700