Springmvc是一个典型的教科书式的mvc构架,而不像struts等都是变种或者不是完全基于mvc系统的框架,对于初学者或者想了解mvc的人来说我觉得 spring是最好的,它的实现就是教科书!第二它和tapestry一样是一个纯正的servlet系统,这也是它和tapestry相比 struts所具有的优势。而且框架本身有代码,看起来容易理解。(这是百度百科中对他的介绍)
OK废话不多说,现在开始正文
首先先下载Maven http://maven.apache.org/download.cgi ,解压完之后随意放到哪一个盘里面,ok
我这里是放在C盘如下图
打开 Myeclipse 中的菜单 Windows 》Preferences 》搜索“maven”,找到“Installations”,把刚才的 Maven 目录添加进去,并勾选。
ok,然后我们就可以开始创建web项目了File———》》new————》》web project
然后我们就可以看到这样的一个目录
ok,到这里之后maven项目雏形就差不多弄好了,接下来,就是配置pom.xml,springmvc所需要的jar包和mybatis的jar包
还有mysql的jar包,废话不多说上xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>YM_web</groupId>
<artifactId>YM_web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name/>
<description/>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.1.6.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>javaee-api</artifactId>
<version>5.0-1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jr</groupId>
<artifactId>jackson-jr-all</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>4.0.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
接下来配置web.xml,我们都知道打开web,项目是一定要配置这个的,我的备注写的够清楚了,又不懂得可以评论私信我
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springMVC</servlet-name> <!-- 表示在WEB-INF下寻找名为springMVC-servlet的拦截器配置文件 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup> <!-- 表示在web应用启动时立刻加载web项目 -->
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern> <!-- 表示对一切请求进行拦截 -->
</servlet-mapping>
<filter>
<filter-name>UrlRewriteFilter</filter-name> <!-- 配置过滤器对url进行伪静态化处理,有利于SEO的搜索 -->
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping> <!-- 配置Url Rewrite的Filter拦截所有请求 -->
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
配置一个过滤器,接下来在WEB-INF下面建立一个springMVC-servlet的xml文件对springmvc拦截器的具体配置
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 配置扫描的包 -->
<context:component-scan base-package="com.controllers" /> <!-- action层代码 -->
<!-- 访问静态资源 -->
<mvc:default-servlet-handler />
<!-- if you use annotation you must configure following setting -->
<mvc:annotation-driven />
<!-- configure the InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/pages/" />
<!-- 后缀 --> <!-- 配置springmvc的跳转目录 -->
<property name="suffix" value=".jsp" />
</bean>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value> <!-- 数组列表转化成json控制格式 -->
</list>
</property>
</bean>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
</list>
</property>
</bean>
</beans>
这样一来springmvc的配置基本都已经弄完了,由上面的配置文件我们可以知道引擎会默认查找com.controllers下面的class文件,也就是我们的action层。
ok,我们写一个小的demo
首先在src/main/java下面新建一个包com.controllers,接下来我们在里面创建一个class为TestConl的类,内容如下:
package com.controllers;
import java.util.HashMap;
import java.util.Map;
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 = "/test")
public class TestConl {
@RequestMapping(value="",method = RequestMethod.POST)
@ResponseBody
public Map<String,Object> hello(){
Map <String,Object> text_map=new HashMap<String, Object>();
text_map.put("title1", "DRONES");
text_map.put("title2", "IMAGING SOLUTIONS");
text_map.put("title3", "INDUSTRIAL");
text_map.put("title4", "COMMUNITY");
text_map.put("title5", "SUPPORT");
text_map.put("title6", "STORE");
return text_map;
}
}
怎么样,很简单吧,就是创建一个map,向里面封装数据,然后直接返回一个map,这个时候springmvc的强大之处就来了,他会直接帮你将map转化成json,然后是前台jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
<base href="<%=basePath%>">
<title>易创医盟网— 高效人脉共享平台</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="高效人脉共享平台,医盟网,医盟,医疗,资源整合">
<meta http-equiv="description" content="多渠道人脉聚合,独创服务工单,客服全程协助,医盟凭借多年在医药行业人脉资源整合的实力和经验
为您提供多渠道寻找人脉的聚合平台,高效接触到目标人脉,我们相信天下没有聚不拢的人脉">
<script type="text/javascript" src="js/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$(function(){
$.ajax({
type: "post",
url:"test/hello",
dataType: "json",
success:function(data){
alert(data.title1);
},error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
}
});
})
</script>
</head>
<body>
<h1>易创医盟网</h1>
</body>
</html>
很简单的一个构造,就是ajax向后台寻求数据,然后我弹出他们其中一个作为检验。
结果如下:
ok,至此简单的springmvc的demo已经完成!
最后再上一个完整的目录结构。