建立Maven项目html
选择Maven快速构建类型java
完善Maven项目坐标属性web
Maven项目建立成功spring
修改pom.xml文件apache
<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>com.MyTest</groupId> <artifactId>SpringDemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringDemo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.2.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0-b01</version> </dependency> </dependencies> <build> <finalName>SpringDemo</finalName> </build> </project>
修改WEB-INF下的web.xml文件api
<?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/web-app_2_5.xsd" 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> <!-- contextConfigLocation:指定的配置文件所在目录,若是不指定,Spring 会加载WEB-INF目录下,符合 *Context.xml 或 spring*.xml 规则的文件 <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/applicationContext.xml</param-value> </context-param> --> <!-- 配置Spring的内容加载监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 字符集过滤器 --> <filter> <filter-name>encodingFilter</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</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
在WEB-INF下建立文件applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/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 "> <bean id="hello" class="com.MyTest.test.HelloTest"> <property name="msg" value="Hello World!"></property> </bean> </beans>
在src/main/java/com/MyTest/test目录下建立HelloTest.java,没有目录则建立服务器
package com.MyTest.test; public class HelloTest { private String msg; public void setMsg(String msg) { this.msg = msg; } public String hello(){ return "MSG:"+msg; } }
再建立一个Servlet文件以方便进行测试,与HelloTest.java同目录,文件名为HelloServlet.javaapp
package com.MyTest.test; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; //拦截地址为hello的请求 @WebServlet(name="HelloServlet",urlPatterns={"/hello"}) public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 1L; //接受客户端get请求,而后输出内容到页面 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取Spring容器 ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext()); //获取HelloTest实例化对象 HelloTest helloTest =applicationContext.getBean(HelloTest.class); //执行hello方法 String msg =helloTest.hello(); //设置响应类型 response.setContentType("text/html;charset=utf-8"); //输出内容到客户端 response.getWriter().write(msg); } }
展现下项目如今的结构maven
最后,将项目打包发布到服务器中,如Tomcat,而后浏览器输入请求地址:http://localhost:8080/SpringDemo/hello ,请求,看到以下内容,就说明项目搭建成功了!