First, create the Maven-archetype-webapp project in Eclipse:
1. New Project Select Maven Project
2. Default, Next
3. Select Maven-archetype-webapp, others remain default
4. After completing the following, click Finish
5. After creating the completed MAVEN project structure is as follows
Where index.jsp error message: Multiple annotations found at this line:-The superclass "Javax.servlet.http.HttpServlet" is not Foun D on the Java
It means that the servlet package is missing, we can import the Javax.servlet-api-3.1.0.jar package, we can handle it in two ways:
1> adding a dependency package to the dependencies in Pom.xml
<!--https://Mvnrepository.com/artifact/javax.servlet/javax.servlet-api-< dependency> <groupId>javax.servlet</groupId> <artifactid>javax.servlet-api</ Artifactid> <version>3.1. 0</version> <scope>provided</scope></dependency>
2> can add a tomcat library to the build path, as follows
Add tomcat for empty, configure sever to enter
Open Eclipse, click on the "Window" menu and select "Preferences" below.
Click the "Server" option and select "Runtime Environments" below.
Click "Add" to add Tomcat.
Click "Next" to select the Tomcat path you have installed.
Configure the server to complete. Then configure the top Buidpath
With the
Add success
So far, a normal MAVEN Web project has been built, as follows:
Second, the configuration Springmvc
1. Add a dependency to spring in Pom.xml pom.xml
<properties> <spring.version>4.3.18.RELEASE</spring.version> </properties>
<dependency> <groupId>org.springframework</groupId> <artifactid>spring-core </artifactId> <version>${spring.version}</version> </dependency> < dependency> <groupId>org.springframework</groupId> <artifactid>spring-web</ artifactid> <version>${spring.version}</version> </dependency> <dependency > <groupId>org.springframework</groupId> <artifactid>spring-webmvc</ artifactid> <version>${spring.version}</version> </dependency>
Error
<properties> <spring.version>4.3.18.RELEASE</spring.version> </properties> Just add this.
2. Edit the Web. xml file
Web. XML content
<web-app version="3.0"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/javaeehttp//java.sun.com/xml/ns/javaee/web-app_3_0.xsd "><display-name>archetype Created Web application</display-name> <servlet> <servlet-name>dis Patcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param -value>classpath:springcontext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name > <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name> ;contextconfiglocation</param-name> <param-value>classpath:springContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener></web-app>
3. Create the Springcontext.xml file, create the Springcontext.xml file in the Src/main/resources package,
<?xml version= "1.0" encoding= "UTF-8"?
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:context= "Http://www.springframework.org/schema/context"
xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "
xsi:schemalocation="
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
<!--search Spring control-->
<context:component-scan base-package= "Com.kad.test" ></CONTEXT:COMPONENT-SCAN>
<!--View page configuration--
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver";
<property Name= "prefix";
<value>/web-inf/views/</value>
</property>
<property name= " Suffix ",
<value>.jsp</value>
</property>
</bean>
</beans>
In Springcontext.xml, Base-package is the package that specifies the Spring controller control, the prefix specifies the view directory, and is set to/web-inf/views, which is where the view directory is placed under Web-inf. The suffix specifies the extension of the view. For example, the "hellospring" view will be placed in the/web-inf/views/hellospring.jsp.
4. Create a spring controller and view
Package Com.kad.test;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestparam;import Org.springframework.web.servlet.ModelAndView; @Controller Public classHellospringcontroller {String message="Welcome to Spring mvc!"; @RequestMapping ("/hello") PublicModelandview showmessage (@RequestParam (value ="name", required =false, DefaultValue ="Spring") (String name) {Modelandview mv=NewModelandview ("hellospring");//Specifies that the view adds the content that you want to show or use to the view, which is used in the pageMv.addobject ("message", message); Mv.addobject ("name", name); returnMV; }}
Hellospringcontroller.java
In the preceding code, the @Controller annotation is the way the spring labels the front controller, @RequestMapping the annotation map Web request to the class or method to be manipulated, @RequestMapping annotations can be used on the class, can also be used in the method, no longer detailed in this, if there is doubt, can Baidu. @RequestParam Annotations Specify parameters for the request. This approach provides a consistent programming style.
In addition to the above code in the Modelandview class to specify a specific view, here is "hellospring", because we configured the front and back of the view in the Springcontext.xml, so here only need to write out the specific name of the view, which specifically specifies: prefix + The view name + suffix, the full view path/web-inf/views/hellospring.jsp, is the location of the view to be displayed.
Project Home index.jsp Content
<%@ 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">"Content-type"Content="text/html; Charset=utf-8"><title>spring4Mvc-helloworld Index page</title>"Hello?name=zhangsan"> Click to jump </a> In the above code, clicking the link to jump is actually a @requestmapping annotation method for the controller defined in our Hellospringcontroller.java, Name=zhangsan is the parameter that the ShowMessage accepts.
Create a hellospring.jsp view under/web-inf/views/,
hellospring.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">"Content-type"Content="text/html; Charset=utf-8"><title>spring4mvc-helloworld</title>${message} ${name}The code above shows the two parameters that we added in the Hellospringcontroller.java ShowMessage method, message and name,
Well, so far we've finished using Maven to build SPRINGMVC, and the entire project is structured like this:
Third, deploy the project to the Tomcat server to run
Home http://localhost:8080/HelloSpringMVC/, which can also be used http://localhost:8080/HelloSpringMVC/index.jsp two effects are the same
Create a SPRINGMVC project using Maven in eclipse