spring mvc 介绍
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如Struts1,Struts2等。
开始实践demo,步骤如下:
步骤1.前提,创建maven project :参考地址 --》 http://blog.csdn.net/u013013553/article/details/78202361
步骤2. 项目目录结构
步骤3:pom.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>springmvcdemo</groupId>
<artifactId>springmvcdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springmvcdemo</name>
<description />
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.servlet.jsp.jstl</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<version>3.1</version>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
步骤4: web.xml的编写
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<display-name>springmvcdemo</display-name>
<servlet>
<servlet-name>spring</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>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
步骤5:applicationContext.xml的编写
context:component-scan节点,此节点有两个属性base-package属性告诉spring要扫描的包,use-default-filters="false"表示不要使用默认的过滤器,此处的默认过滤器,会扫描包含Service,Component,Repository,Controller注解修饰的类
<context:component-scan base-package="com.demo" />
<!-- 处理在类级别上的@RequestMapping注解 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!-- 处理方法级别上的@RequestMapping注解 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!--ModelAndView对象中的view对象,可以使用字符串来让Spring框架进行解析获得适合的视图。而解析View的就是ViewResolver技术。
InternalResourceViewResolver将逻辑视图名字解析为一个路径-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan base-package="com.demo" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
步骤6:实体类的编写, Student.java
package com.demo.model;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
}
步骤7:数据库操作接口的编写,StudentDao.java
package com.demo.dao;
import java.util.List;
import com.demo.model.Student;
public interface StudentDao {
public List<Student> getAllStudents();
}
步骤8:实现上面StudentDao接口,StudentDaoImpl.java
package com.demo.dao;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.demo.model.Student;
//@Repository用于标注数据访问组件,即DAO组件
@Repository
public class StudentDaoImpl implements StudentDao {
@Override
public List<Student> getAllStudents() {
List<Student> students = new ArrayList<Student>();
Student stu1 = new Student();
stu1.setId(1);
stu1.setName("Zhang San");
students.add(stu1);
Student stu2 = new Student();
stu2.setId(2);
stu2.setName("Li Si");
students.add(stu2);
return students;
}
}
步骤9:service层的编写,调用刚刚dao的实现,这里也是serviceceng的接口,StudentManager.java
package com.demo.service;
import java.util.List;
import com.demo.model.Student;
public interface StudentManager {
public List<Student> getAllStudents();
}
步骤10:service层的实现,实现上面service层的接口,StudentManagerImpl.java
package com.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo.dao.StudentDao;
import com.demo.model.Student;
@Service
public class StudentManagerImpl implements StudentManager {
@Autowired
StudentDao dao;
public List<Student> getAllStudents() {
return dao.getAllStudents();
}
}
步骤11:controller层的编写,接受前端传来的请求,调用service层,StudentController.java
package com.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.demo.service.StudentManager;
//@Controller 负责注册一个bean 到spring 上下文中,bean 的ID 默认为类名称开头字母小写
@Controller
@RequestMapping("/student-module")
public class StudentController {
// @Autowired 根据bean 类型从spring 上线文中进行查找,注册类型必须唯一,否则报异常。
@Autowired
StudentManager manager;
//@RequestMapping 可以声明到类或方法上
@RequestMapping(value = "/getStudentInfo", method = RequestMethod.GET)
public String getStudentInfo(Model model) {
model.addAttribute("students", manager.getAllStudents());
return "showStudentInfo";
}
}
步骤12: 编写前端jsp页面, showStudentInfo.jsp,这个jsp文件存在于views文件夹下,其配置在applicationcontext.xml文件中<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">已经指定了。
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<html>
<head>
<title>Spring MVC Demo</title>
</head>
<body>
<h2>All Students</h2>
<table border="1">
<tr>
<th>Student Id</th>
<th>Student Name</th>
</tr>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
步骤13: 将项目部署到tomcat服务器上
步骤14: 通过浏览器进行访问 , http://localhost:8080/springmvcdemo/student-module/getStudentInfo,得到结果
github项目地址: https://github.com/heimohe/springmvc_demo/