In the previous chapter, the simplest way to create a service-side code was used, but it was generally used in the actual development of a more mature framework. Here's how the full maven+spring MVC creates the service
Download install Eclipse and JDK
It is important to note that the number of bits of the Eclipse JDK selection version is always. Not one is a 32-bit one is 64 bits, inconsistency can cause later use error
After completing the download, you will need to configure the JDK environment variable JDK7 configuration jdk10 configuration.
Download install MAVEN and configure environment variables
We need to configure the user scope after the completion setting.xml
安装文件夹/conf/setting.xml是全局的设置,这个电脑上全部的用户都会受影响,所以将安装文件夹/conf/setting.xml复制一份,到上面配置仓库位置中,默认不设置路径在c:/用户/用户名/.m2下
The high version of Eclipse defaults to a maven plugin. Let's say we can download it ourselves.
Http://m2eclipse.sonatype.org/sites/m2e
Configure Eclipse
In Window--preferences--maven
Installations tick off the default Maven,add installed Maven folder
In user settings, select the Setting.xml for the local warehouse configuration. Like my E:\m2\settings.xml.
Build your project with MAVEN select new Maven project in Eclipse. Choose your work environment
1. Select the filter you want to create such as the following:
2. Fill in MAVEN project coordinate information
The group ID here is the id,arifact ID of the large item. It's like a big project with a lot of small projects.
The structure of the project we build such as the following
Intact Project
First, the Intact folder, add the important source folder, which is used in the following config build path. Need to add
Add Src/main/resources. Src/test/resources folder. Make the folder into a standard MAVEN structure.
Convert a project to a Web project
Select the project properties. For example, to join the web Module
Join the Src/main/webapp path. Select the following options as well. Click OK when you are done
When you click Apply
Then right click on Project familiarity
Get rid of the extra items and add the new folder you just created.
For example with
The final complete project works such as the following
XML configuration file
Pom.xml content such as the following
<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>MavenTest</groupId> <artifactid>tan.maven.springmvc</ Artifactid> <packaging>war</packaging> <version>0.0. 1-snapshot</version> <name>tan.maven.springmvc maven webapp</name> <url>http://maven.apache.org</url><properties> <project.build.sourceencoding>utf-8</project.build.sourceEncoding> <springversion>3.1. 1. Release</springversion> <junitversion>3.8. 1</junitversion> </properties> <dependencies> <dependency> <groupid>j Unit</groupid> <artifactId>junit</artifactId> <version>${junitversion}</v ersion> <scope>test</scope> </dependency> <dependency> < Groupid>org.springframework</groupid> <artifactId>spring-aop</artifactId> <v Ersion>${springversion}</version> <type>jar</type> <scope>compile</sco pe> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-asm</artifactId> <version>${springversion}</version> <type>jar</type> <scope>compile</scope> </dependency> <de Pendency><groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${springversion}</version> <type>jar</type> <scope>compile </scope> </dependency> <dependency> <groupid>org.springframework</grou Pid> <artifactId>spring-beans</artifactId> <version>${springversion}</versio n> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-c Ontext</artifactid> <version>${springversion}</version> <TYPE>JAR</TYPE&G T <scope>compile</scope> </dependency> <dependency> <groupid>org.spri NgframeworK</groupid> <artifactId>spring-context-support</artifactId> <version>${sprin gversion}</version> <type>jar</type> <scope>compile</scope> < ;/dependency> <dependency> <groupId>org.springframework</groupId> <ar Tifactid>spring-core</artifactid> <version>${springversion}</version> <type& gt;jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <ARTIFACTID>SPRING-EXPRESSION</ARTIFACTID&G T <version>${springversion}</version> <type>jar</type> <scope>compile< ;/scope> </dependency> <dependency> <groupid>org.springframework</groupid > <artifactId>spring-jdbc</artifactId> <version>${springversion}</version> <type>jar</type> <scope>compile</scope> </dependency> <depend Ency> <groupId>org.springframework</groupId> <ARTIFACTID>SPRING-JMS</ARTIFAC Tid> <version>${springversion}</version> <type>jar</type> < scope>compile</scope> </dependency> <dependency> <GROUPID>ORG.SPRINGFR Amework</groupid> <artifactId>spring-orm</artifactId> <version>${springversi on}</version> <type>jar</type> <scope>compile</scope> </depe ndency> <dependency> <groupId>org.springframework</groupId> <artifact Id>spring-oxm</artIfactid> <version>${springversion}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupid>org.spri Ngframework</groupid> <artifactId>spring-tx</artifactId> <version>${springve rsion}</version> <type>jar</type> <scope>compile</scope> </d ependency> <dependency> <groupId>org.springframework</groupId> <artif Actid>spring-web</artifactid> <version>${springversion}</version> <type>j ar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${sPringversion}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> &L T;artifactid>spring-test</artifactid> <version>${springversion}</version> <t ype>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> & Lt;version>1.2</version> <type>jar</type> <scope>compile</scope> </depende ncy> <dependency> <groupId>commons-collections</groupId> <artifactid& Gt;commons-collections</artifactid> <version>3.1</version> </dependency> <dependency> <groupid>commons-logging</groupi D> <artifactId>commons-logging</artifactId> <version>1.1</version> </dependency> </dependencies> <build> <FINALNAME>TAN-SPRINGMV C-book</finalname> </build></project>
Web. XML is located under the Web-inf path
<?xml version="1.0"encoding="UTF-8"? ><web-app xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xmlns="Http://java.sun.com/xml/ns/javaee"xmlns:web="Http://java.sun.com/xml/ns/javaee"xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"> <display-name>archetype Created Web application</display-name> <servlet> <!--the core of spring MVC is Is Dispatcherservlet, the first step in using SPRINGMVC is to put the following servlet into the Web. Xml Servlet-name property is very important. By default, Dispatchservlet will load the file with the name-servlet.xml, such as the following, and it will be loaded into Dispather-servlet.xml, also under the Wen-inf folder. can also directly specify detailed files <servlet> <servlet-name>book</servlet-name> <servlet-class>org.sp Ringframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name& Gt;contextconfiglocation</param-name> <param-value>/web-inf/applicationcontext.xml</param-valu E> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-name>dispatcher</servlet-name> < ;servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup >1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name > <url-pattern>/</url-pattern> </servlet-mapping> <filter> <FILTER-NAME>ENCODINGF Ilter</filter-name> <filter-class>org.springframework.web.filter.characterencodingfilter</ filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingfilter& Lt;/filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <display-name>mvc</display-name> < Welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</ Welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</ Welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</ Welcome-file> </welcome-file-list></web-app>
Dispatcher-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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/mvc Http://www.springframework.org/schema/mvc/spring-mvc. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd " default-lazy-init="true"> <!--set up static resources through mvc:resources so that the servlet processes these static resources. Instead of the controller--<!--settings just filter content, for example: Css,jquery,img and other resource files--<mvc:resources location="/*.html"mapping="/**.html"/> <mvc:resources location="/css/*"mapping="/css/**"/> <mvc:resources location="/js/*"mapping="/js/**"/> <mvc:resources location="/images/*"mapping="/images/**"/> <!--add annotations driver-<mvc:annotation-driven/> <!--default scanned package path--<context:compon Ent-scan base- Package="Exam_1"/> <!--Mvc:view-controller is able to handle request without a controller. Turn to the Settings view-<!--set up like this. Suppose the request is/. Without a controller, directly resolves to/index.jsp---<mvc:view-controller path="/"View-name="Index"/> <bean class="Org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="Viewclass"Value="Org.springframework.web.servlet.view.JstlView"></property> <!--configuring JSP path Prefixes--<property name="prefix"Value="/"></property> <!--configuration URL suffix--<property name="suffix"Value=". JSP"></property> </bean> </beans>
In
Create Java class Mycontroller
PackageMvcImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.bind.annotation.ResponseBody;@Controller Public class mycontroller { @RequestMapping("Login")//To handle login requests from the foreground Private@ResponseBody StringHello( @Requestparam(Value ="username", required =false) String username,@RequestParam(Value ="Password", required =false) String password) {return "Hello"+username+", Your password is:"+password; } }
To the completion of this code writing
Project execution
Very many Bolg articles come to an end here, because those people have already developed projects within their weorkspace.
Let's say we've just downloaded 2 parts.
The first step right-click the project run as-> Maven install to load the required class library
Second section Tomact property settings
Run as->run on Server Select your tomact and add the item in. Assume that there is a 404 error when executing without errors
The workaround is as follows:
Right-click to select Add and Reomve. Then double-click to pop up the following page
Configure the following two items again
And then execute the project and it's OK.
The reason for this is more specific.
Project Source code Address
Servlet and Android Network Interaction Basics (3)