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.');
		}
	});
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值