Teach you to create a Maven+Spring MVC HelloWord project with eclipse

   1. Create a new maven project   


                 


    Click next


                


                    


                                        


            This is the directory of the project


                                

        


        We will find that the project is wrong, and then solve the error (if it is correct, please skip it)


                    


                


                                        

            

                                            


                  


    Next, I found that the project has no errors


        


            


        Note: As a Maven project, the following 4 resource folders must be included, if not, you need to create them manually.

    

                                


        We will find that our project has a problem


                                    


Next create




                


        



        


                                    


                  



                    



                                




                    


The configuration corresponds to this


                    



            



The next step is to change the pom.xml file (add the jars required by springmvc)


        

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.hello</groupId>
  <artifactId>HelloSpringMVC</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>HelloSpringMVC Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- spring start -->  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-aop</artifactId>  
        <version>4.3.16.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-aspects</artifactId>  
        <version>4.3.16.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-beans</artifactId>  
        <version>4.3.16.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-context</artifactId>  
        <version>4.3.16.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-context-support</artifactId>  
        <version>4.3.16.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-core</artifactId>  
        <version>4.3.16.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-expression</artifactId>  
        <version>4.3.16.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-jdbc</artifactId>  
        <version>4.3.16.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-orm</artifactId>  
        <version>4.3.16.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-test</artifactId>  
        <version>4.3.16.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-tx</artifactId>  
        <version>4.3.16.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-web</artifactId>  
        <version>4.3.16.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-webmvc</artifactId>  
        <version>4.3.16.RELEASE</version>  
    </dependency>  
      
    <!-- spring end -->  
    
  </dependencies>
  <build>
    <finalName>HelloSpringMVC</finalName>
  </build>
</project>



Change web.xml file configuration


        

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  
   <!-- Front Controller-->  
    <servlet>    
        <servlet-name>spring-mvc</servlet-name> <!-- name, the real file name needs to add -servlet suffix to this name -->  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--  DispatcherServlet路径,照写 -->  
         
        <!-- Here you can set the path of the front-end controller. If this step is commented out, the default path of the front-end controller is: /WEB-INF/[servlet-name]-servlet.xml  
       <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>/WEB-INF/configs/spring-mvc-servlet.xml</param-value> Here is the path to configure the front controller  
        </init-param>    
        -->  
          
        <load-on-startup>1</load-on-startup>  
    </servlet>    
      
    <!-- Intercept all requests -->  
    <servlet-mapping>    
        <servlet-name>spring-mvc</servlet-name> <!-- The name here should be the same as the servlet-name above -->  
        <url-pattern>/</url-pattern>    
    </servlet-mapping>    
</web-app>


New front controller configuration file

Because the default path is used here, only the front controller file needs to be created in the WEB-INF directory.

And because the name configured in web.xml is spring-mvc, the new file name is spring-mvc-servlet.




        


        


    Next is the configuration content of spring-mvc-servlet.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
		
 <!-- scan path -->  
    <context:component-scan base-package="com.hp.controller" >  
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
    </context:component-scan>  
  
    <!-- Configure root view -->  
    <mvc:view-controller path="/" view-name="index"/>  
  
    <!-- activate annotation-based configuration @RequestMapping, @ExceptionHandler, data binding, @NumberFormat ,  
    @DateTimeFormat ,@Controller ,@Valid ,@RequestBody ,@ResponseBody等  -->  
    <mvc:annotation-driven />  
  
    <!-- Static resource configuration-->  
    <mvc:resources location="/assets/" mapping="/assets/**"></mvc:resources>  
  
    <!-- View Layer Configuration -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/view/"/>  
        <property name="suffix" value=".jsp"/>  
    </bean>  
  


</beans>


      The next step is to create the controller class




Among them, it should be noted that




        

Next is the writing of spring-mvc-servlet.java


package com.hp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("hello")
public class SpringController {
	@RequestMapping("one")
	public String Hello(Model model){
		model.addAttribute("msg", "This is my first springMVC project");
		return "text";
	}

}


Next, write the returned jsp page


    


                        



            


       New text.jsp


<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>This is a jsp</title>
</head>
<body>
<h1>${msg }</h1>
</body>
</html>

If the item appears red x at this time


        (1) Right-click the project name -> Properties (at the bottom) -> drag the left column to find Deployment Assembly -> remove the excess.


                


   (2) Remove Maven Dependencies from Build Path.


        


(3) Go to Configure Build Path and re-add the Library of Maven Dependencies









正常情况,项目已经没有不报错了。


运行项目




    效果图为




总结一下:

由于第一次使用maven,刚开始有点生疏,其中pom.xml只要进行了配置jar包,然后maven会自动在阿里云的

maven仓库进行下载,就不用管jar,在springMVC开发过程中也遇到了controller层的@controller一直没有注释

但是我的pom.xml文件已经进行了配置,错误的原因是 pom.xml的jar版本有点低 ,我把pom.xml文件中的jar版本提升到4.3.16.RELEASE然后顺利的解决了这个bug.


在第一次运行maven你有可能看到这个错误


                


解决办法是

                        


Add -> Java Build Path Entries -> Next -> Maven Dependencies -> Finish -> Ok,配置完后如下图:


        


这是我的源码 https://download.csdn.net/download/qq_40646143/10359916


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326080527&siteId=291194637