1. Eclipse creates MAVEN project
Error after creation: The superclass "Javax.servlet.http.HttpServlet" is not found on the Java Build Path
Workaround: Right-click the project-->propertites--->project facets---Run time to tick the configured Tomcat, clicking OK, the error disappears
If you do not have Tomcat installed, please install it yourself.
If Maven Dependency is not displayed in the project, it will be displayed when added to the local classpath file
<classpathentry kind= "con" path= "Org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
2. Increase the SPRINGMVC required jar package
Two maven dependencies, Spring-context;spring-mvc. Maven automatically downloads all associated jar packages, including
- Spring-webmvc
- Spring-beans
- Spring-core
- Spring-expression
- Spring-web
- Spring-context
- Spring-aop
- Aopalliance
- Commons-logging
So just add the following in the Pom file:
<!--Configuring the Spring MVC jar - <!--Https://mvnrepository.com/artifact/org.springframework/spring-webmvc - <Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-webmvc</Artifactid> <version>4.3.3.RELEASE</version> </Dependency> <!--Https://mvnrepository.com/artifact/org.springframework/spring-context - <Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-context</Artifactid> <version>4.3.3.RELEASE</version> </Dependency>
Problem:
Maven prompts invalid LOC header (bad signature) workaround, delete the repository jar file, update project again
Add 3 source folder to the project, Src/main/java, Src/main/resources,src/test/java, respectively. The project is as follows:
Add the Src/main/webapp directory to the project, copy the files in the WebApp directory in the source folder and delete the src that you created. Complete the WebApp directory as follows
Configure the WebApp directory as the Webroot directory,
After the configuration is complete, modify the Web. xml file as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "3.1"xmlns= "Http://xmlns.jcp.org/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/we B-app_3_1.xsd "> <Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>/web-inf/applicationcontext.xml</Param-value> <!--default is/web-inf/applicationcontext.xml - </Context-param> <Listener> <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class> </Listener> <servlet> <Servlet-name>Springmvc</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>/web-inf/springmvc-servlet.xml</Param-value> <!--The default is/web-inf/[servlet name]-servlet.xml - </Init-param> <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Springmvc</Servlet-name> <Url-pattern>/</Url-pattern> </servlet-mapping> </Web-app>
Create Applicationcontext.xml and Servletname-servlet.xml under the Web-inf directory
Applicationcontext.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/sp Ring-beans-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX http://www . springframework.org/schema/tx/spring-tx-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spring-aop-4.0.xsd HTTP://WWW.SPR Ingframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0 . xsd "></Beans>
Springmvc-servlet.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spri Ng-beans-4.0.xsd Http://www.springframework.org/schema/mvc Http://www.spri Ngframework.org/schema/mvc/spring-mvc-4.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www. SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-4.0.xsd Http://www.springframework.org/schema/tx HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/T X/spring-tx-4.0.xsd "> <!--set the jar package for the class where the annotations are used - <Context:component-scanBase-package= "Hello" /></Beans>
Add Contorller Class
package Hellotest; import Org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import Org.springframework.web.bind.annotation.ResponseBody; @Controller public class Hellocontroller {@ Requestmapping ( "/hello" ) public @ResponseBody String Testhello () { retur n "hello,spring mvc" ; }}
In Springmvc-servlet.xml Add the jar package where the class using the annotations is set
<context:component-scan base-package="hellotest"/>
Deploy Tomcat to perform tests
At this time, all the environmental construction work has been completed, citing Xingoo's blog summarized as follows:
12 maven dependencies, Spring-context;spring-mvc. Maven automatically downloads all associated jar packages, including
- Spring-webmvc
- Spring-beans
- Spring-core
- Spring-expression
- Spring-web
- Spring-context
- Spring-aop
- Aopalliance
- Commons-logging
21 Web. xml files with listener and servlet configured
32 Spring related files, applicationcontext.xml and Servletname-servlet.xml
41 controller files, configured with blocked URL handling code
"Spring Combat 4" 10--build SPRINGMVC Environment