Building the Spring MVC Project with Eclipse (illustrated)

Source: Internet
Author: User
Tags aop naming convention log4j

Recently, I need to quickly understand the spring mvc architecture, so I found many related tutorials on the Internet. Seriously, the basics of Baidu are the same, but I have encountered all kinds of strange problems. Of course, looking back after doing it, I found that those problems are not problems. Hahahaha ~ I still do n’t understand java. When I encounter a problem, I find it difficult. Various investigations. Helpless, different URLs, the same content. Build spring mvc project with Eclipse (Illustration)

I refer to a blog to build the project, the URL is: http://limingnihao.iteye.com/blog/830409. The project was created according to the process of this blog, but due to the different actual conditions, there are still some differences in the middle.

Let me list the relevant software versions I use. Although I do n’t know what these have to do, let me list them. Use Eclipse to build a spring mvc project (illustration)

Use Eclipse to build spring mvc project (Illustration) Use Eclipse to build spring mvc project (Illustration)
Because I do n’t know which columns, so I cut these pictures to build a spring mvc project (illustration) with Eclipse, only know to use jdk, maven, tomcat, and java compiler (such as eclipse)

Start construction now

1. Create a maven project

 Build spring mvc project with Eclipse (Illustration)



Build spring mvc project with Eclipse (Illustration)




Build spring mvc project with Eclipse (Illustration)



Build spring mvc project with Eclipse (Illustration)


Build spring mvc project with Eclipse (Illustration)


Build spring mvc project with Eclipse (Illustration)


Build spring mvc project with Eclipse (Illustration)

Build spring mvc project with Eclipse (Illustration)


Build spring mvc project with Eclipse (Illustration)




At this point, a mave project has been built. Let's configure some things now.

2. Configure pom.xml (can be directly added to pom.xml, configure some things in seven or eight. Junit, jstl, spring package)

<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>
The
<dependency>
<groupId> jstl </ groupId>
<artifactId> jstl </ artifactId>
<version> 1.2 </ version>
</ dependency>
The
  <!-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 dependencies End->
  </ dependencies>

3. Add a folder "WEB-INF" under webapp, and add a file "web.xml" under the folder (the following content can be copied 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">
<!-Distinguish the project name, prevent the default duplicate name->
    <context-param>
        <param-name> webAppRootKey </ param-name>
        <param-value> maven.example.root </ param-value>
    </ context-param>
  
    <!-Spring's log4j listener->
    <listener>
        <listener-class> org.springframework.web.util.Log4jConfigListener </ listener-class>
    </ listener>
  
    <!-Character set filter->
    <filter>
        <filter-name> CharacterEncodingFilter </ filter-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>
            <param-value> true </ param-value>
        </ init-param>
    </ filter>
    <filter-mapping>
        <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.servlet.DispatcherServlet </ servlet-class>
        <init-param>
            <param-name> contextConfigLocation </ 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> *. do </ url-pattern>
    </ servlet-mapping>

</ web-app>
There are several notable places in the web.xml file. The red "dispatcher0121" above can be modified to the string you want, but these three places must be the same. [servlet-name] + "-servlet.xml" This is the naming convention of the file name of the spring configuration file.
4. Add another file "dispatcher0121-servlet.xml" under "webapp / WEB-INF /", this file is what is said above (can all be copied to the newly created 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/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-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 = "example0121" />
  
    <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name = "prefix" value = "/ WEB-INF / views /" />
        <property name = "suffix" value = ". jsp" />
    </ bean>
  
</ beans>

There are also points to note in the dispatcher0121-servlet.xml file. "example0121" is the package that will be used next. The name can be arbitrary, but not all numbers. I do n’t want to say that I ’m just lazily using numbers, which has caused problems with the previously built projects. Build spring mvc project with Eclipse (Illustration)
Build spring mvc project with Eclipse (Illustration)

Picture 10

5. Create a Controller layer test class (I do n’t know what this is. I only know how to operate it)


Build spring mvc project with Eclipse (Illustration)
Picture 11

According to this operation, you can add a package or a class. First add the class. If you find that "example0121" is not written in the package, cancel it, and then add a package. The name of the package is "example0121". Then create a class called "GeneralController".

Build spring mvc project with Eclipse (Illustration)

Picture 12

Then write the code, like below

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 pages. Add a folder "views" under "webapp / WEB-INF /" and add a file "index.jsp" This path is set in "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">
<html>
    <head>
        <meta http-equiv = "Content-Type" content = "text / html; charset = UTF-8">
        <title> Insert title here </ title>
    </ head>
      
    <body>
        <c: out value = "$ {str0121}"> </ c: out>
    </ body>
</ html>
At this time, you can see a red cross in your project and report an error, as shown in Figure 13. At this time you have to calm down, two methods. One is to ignore it, and the other is to delete this mistake. I chose to ignore it. I was searching for information for a long time because of this error, and I was inexplicably irritated. I tried the method of saying it on the Internet. Finally, I asked a colleague, and the colleague said, can you run it or not? I ran the project and found it works. Then the colleague said, then ignore it. It seems that it is a problem of verification, which is not affected anyway. one or two. .
Build spring mvc project with Eclipse (Illustration)


7. Run

Build spring mvc project with Eclipse (Illustration)



Build spring mvc project with Eclipse (Illustration)


Build spring mvc project with Eclipse (Illustration)


Build spring mvc project with Eclipse (Illustration)

Build spring mvc project with Eclipse (Illustration)




The project is now created. To start a deep discussion on building a spring mvc project with Eclipse (illustration) building a spring mvc project with Eclipse (illustration) building a spring mvc project with Eclipse (illustration)

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.