Spring+maven+eclipse Building Web Engineering

Source: Internet
Author: User

1 Environment Preparation
    • Download eclipse:http://www.eclipse.org/downloads/packages/release/neon/r, use the neon,32-bit version of Windows, download and unzip;

    • Download maven:http://maven.apache.org/download.cgi, download the version 3.3.9, unzip directly to a directory

    • Spring does not download
    • Download Tomcat8.0 and unzip it to a directory without installing

    • To configure Maven in eclipse:

    • To configure Tomcat in eclipse

2 Creating a Project
    • Create a new MAVEN project, archetype Select WebApp, set GroupID and Artifactid.

After the completion of the project catalog as follows:

    • As you can see, the new project still has some things to adjust, the steps are as follows:
      • Add the corresponding source and resource directory, adding/main/java directory operations and add/main/java and/test/java modified results are as follows:

      • To adjust the JDK version, the JDK1.8 is used here, the key is not to choose the JRE:

First go to the Preference menu to adjust the JRE directory to point to the JDK directory, such as

Libraries of the Adjustment project:

3 Configuration and code writing
    1. Configuration pom.xml: Because of the use of SPRINGMVC, JSTL, etc., it is necessary to add a dependency on it, it is worth noting that there is no need to actively increase the dependence on springcore, because we do not directly use, Maven will automatically download SPRINGMVC and its dependent springcore such as the jar package (this process will be slow, but the domestic maven image seems to have some problems, do not understand how to use)

After automatic download, the dependent MAVEN packages include:

    • Modify Web. xml: Because Maven WebApp archetype older, the default is to use the jsp1.2 standard, which will cause the JSP page default can not use El expression and so on, the solution see http://www.2cto.com/kf/ 201404/293482.html, here is the second way to use the jsp2.0 header directly in Web. XML, and the contents of the modified Content Web. XML are as follows:

    • Config spring servlet and servlet-mapping in Web. XML, complete with Web. Xml as follows
<Web-appversion= "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 the Spring-servlet.xml content, because the path is not specified in Web. XML, so spring-servlet.xml needs to be placed in the/web-inf directory, as follows.

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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 http://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-c Ontext-3.0.xsd Http://www.springframework.org/schema/mvc Http://www.springframework.org/schema/mvc/spring-mvc -3.0.xsd " >          <!--Configuring support Annotations -    <Mvc:annotation-driven/>   <!--Configure the controller to automatically scan the package name for spring to automatically load the controller -     <Context:component-scanBase-package= "Stock.controller"></Context:component-scan>    <!--Configure the default viewresolver because you want to use JSTL, so you have configured Viewclass for Org.springframework.web.servlet.view.JstlView In addition, the prefix suffix for view is configured, The prefix is/web-inf/views, so the subsequent JSP pages need to be placed in the/web-inf/views directory -    <BeanID= "Defaultviewresolver"class= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "Viewclass"value= "Org.springframework.web.servlet.view.JstlView"/>        < Propertyname= "ContentType"value= "text/html"/>              < Propertyname= "prefix"value= "/web-inf/views/"/>        < Propertyname= "suffix"value= ". jsp"/>    </Bean>    </Beans>
    • Create a new controller with the following code: use annotations to configure its matching URL as/or/index, and the code to add a string object with key "Welcomewords" in the model
 PackageStock.controller;ImportJava.util.Map;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping; @Controller Public classHomeController {@RequestMapping ({"/", "/index"})     PublicString Showhomepage (map<string,object>model) {Model.put ("Welcomewords", "Welcome, boss."); //System.out.println ("Showhomepage executed, using stock");                return"Index"; }}
    • To modify the index.jsp and move to the/web-inf/views directory, the index.jsp content 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 ">    <HTML>        <Head>            <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">            <title>Test Home 2</title>        </Head>                <Body>            <H1>${welcomewords}</H1>    </Body>    </HTML> 

4 Testing

Right-click the project and select run on server for run as, because server is not configured, the configuration interface appears:

Next: Stock items are automatically on the right

Start running, index.jsp page ${welcomewords} was replaced with the "welcome you, Boss" set in HomeController. Get!

Spring+maven+eclipse Building Web Engineering

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.