Spring項目搭建

  • 建立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   ,請求,看到以下內容,就說明項目搭建成功了!

相關文章
相關標籤/搜索