Spring MVC 項目搭建 -1- 創建項目

1.創建 Dynamic Web project (SpringDemo),添加相關jar包

2.創建一個簡單的controller類

package mvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/mytest")
public class TestController{
	@RequestMapping(value="/helloSpring",method=RequestMethod.GET)
	public @ResponseBody String HelloWorld(){
		return "Hello Spring";
}

3.創建

web.xml 

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
	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_2_5.xsd">
 	<!-- web 項目名 -->
 	<display-name>SpringDemo</display-name>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:applicationContext.xml
		</param-value>
	</context-param>
	<!-- 用於初始化自定義配置 context-param-->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener> 
	 
	<!--創建默認首頁  -->
	<welcome-file-list>
	  <welcome-file>index.html</welcome-file>
	</welcome-file-list>
	
	<!--初始化 spring mvc 配置 ,創建一個servlet 映射-->
	<servlet>  
		<!-- 配置文件命名爲 {servlet-name} + '-servlet.xml',路徑爲WEB-INF下 -->
        <servlet-name>spring-test</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>spring-test</servlet-name> 
         <!-- 需要加前綴纔可以被該 servlet 攔截  eg: */SpringDemo/test/mytest/login--> 
        <url-pattern>/test/*</url-pattern>  
    </servlet-mapping>
</web-app>


spring-test-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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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">

	<mvc:annotation-driven />
	<!-- 添加 MVC注入配置掃描 -->  
	<context:component-scan base-package="mvc" />
</beans>

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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">		
</beans>

4,在瀏覽器上調用該controller 或者寫一個簡單的前端(index.html,只有一個按鈕),點擊調用後臺並且彈出返回內容

index.js

$(function(){
	$('#btn-test').click(function(event){
		test();
	});
});

function test(){
	$.ajax({
		type : "GET",
		url : "./test/mytest/helloSpring",
		success : function(result){
			alert(result)
		},
		error: function(){
			$().toastmessage('showWarningToast', 'Please try again.');
		}
	});
};



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章