maven项目中搭建spring框架

1.首先看一下,项目结构:

maven项目中搭建spring框架_第1张图片

 

2.在pom.xml添加依赖,这样spring所需要的jar包就自动下载到项目中了

maven项目中搭建spring框架_第2张图片

<!-- 依赖spring,导入spring所需jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.9.2.RELEASE</version>
</dependency>

 

3.web.xml配置

maven项目中搭建spring框架_第3张图片

<!-- 配置spring的servlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- Spring的log4j监听器 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<!-- 字符集 过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 欢迎页 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

 

4.applicationContext.xml配置

maven项目中搭建spring框架_第4张图片

<!-- viewresolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/">
</property>
<property name="suffix" value=".jsp">
</property>
</bean>

<!-- 使用如下两个class激活@RequestMapping annotation -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<!-- 扫描service,controller -->
<context:component-scan
base-package="com.zp.test"/>

<!-- handlermapping @RequestMapping,
@ResponseBody,@ExceptionHanlder -->
<mvc:annotation-driven/>

 

5.编写jsp页面

maven项目中搭建spring框架_第5张图片

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="js/jquery-1.10.1.min.js"></script>
<script>
function test(){

$.ajax({
url:"/test2/test.do",
data:{},
type:"post",
dataType:"json",
success:function(data){
alert("success");
},
error:function(){
alert("error");
}
});


}

</script>
<body>
<input type="button" value="test" onclick="test();"/>
</body>
</html>

 

6.编写Controller

maven项目中搭建spring框架_第6张图片

@Controller
public class TestController {

@RequestMapping("/test.do")
@ResponseBody
public Map<String,Object> test(){
System.out.println("测试");
String s = "ok";
Map<String,Object> resMap=new HashMap<String,Object>();
resMap.put("code", s);
return resMap;
}

}

 

7.启动服务,运行项目

maven项目中搭建spring框架_第7张图片

 

你可能感兴趣的:(maven项目中搭建spring框架)