Spring + Maven + Eclipse build Web project, maveneclipse
1. Prepare the environment
- Download Maven: http://maven.apache.org/download.cgi. download 3.3.9then and directly decompress it to a directory.
- Spring does not need to be downloaded
- Download Tomcat8.0 and decompress it to a directory without Installation
- Configure Maven in Eclipse:
- Configure Tomcat in Eclipse
2. Create a project
- Create a Maven project, select webapp for archetype, and set groupId and artifactId.
After the project is created, the project directory is as follows:
- As you can see, there are still some changes to the new project. The steps are as follows:
- Add the corresponding source code and Resource Directory, add the/main/java directory, and add the/main/java and/test/java directories. The modified results are as follows:
-
- To adjust the JDK version, JDK 1.8 is used here. The key is not to select JRE:
First, go to the Preference menu and adjust the JRE directory to point it to the JDK directory, as shown in
Adjust the Libraries of the project:
3. Configuration and code writing
After automatic download, the dependent Maven package includes:
- Modify web. xml: Because the archetype of maven webapp is old, the jsp1.2 standard is used by default. Therefore, the el expression cannot be used for jsp pages by default. For the solution, see release:
- Configure Spring servlet and servlet-mapping in web. xml. The complete web. xml is as follows:
<web-app version="2.5" 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"> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>
- Create and configure spring-servlet.xml content because paths are not specified in web. xml, so the spring-servlet.xml needs to be placed in the/WEB-INF directory as follows,
<? 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: p = "http://www.springframework.org/schema/p" xmlns: context = "http://www.springframework.org/schema/context" xmlns: util = "http://www.springframework.org/schema/util" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemaLocation = "http://www.springframework.org/schema/util ht Tp: // www.springframework.org/schema/util/spring-util-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 "> <! -- Support annotation configuration --> <mvc: annotation-driven/> <! -- Configure the controller automatic scan package name to enable spring to automatically load controller --> <context: component-scan base-package = "stock. controller "> </context: component-scan> <! -- Configure the default viewResolver. Because jstl is used, viewClass is configured as org. springframework. web. servlet. view. jstlView In addition, configured with the view prefix suffix, prefix for/WEB-INF/views, so the subsequent jsp page needs to be placed in the/WEB-INF/views directory --> <bean id = "defaultViewResolver" class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" viewClass "value =" org. springframework. web. servlet. view. jstlView "/> <property name =" contentType "value =" text/html "/> <property name =" prefix "value ="/WEB-INF/views/"/> <property name = "suffix" value = ". jsp "/> </bean> </beans>
- Create a new controller. The Code is as follows: configure the matched URL as // index using annotations, and add a String object whose key is "Welcomewords" to the model in the code.
Package stock. controller; import java. util. map; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; @ Controllerpublic class HomeController {@ RequestMapping ({"/", "/index"}) public String showHomePage (Map <String, Object> model) {model. put ("WelcomeWords", "Welcome, Boss"); // System. out. println ("showHomepage executed, using stock"); return "index ";}}
- Modify index. jsp and move it to the/WEB-INF/views directory. The content of index. jsp is as follows:
<% @ 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">
4 Test
Right-click the project and select Run On Server of Run as. The configuration page is displayed because no Server is configured:
Next: the stock project is automatically displayed on the right.
Start running. The $ {WelcomeWords} on the index. jsp page is replaced with "Welcome, Boss" set in HomeController ". Done!