Spring MVC入门Demo
1 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为springmvcdemo的Maven工程。
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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
- id="WebApp_ID" version="3.1">
- <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中的完整内容为:
- <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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.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 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
- public class StudentDaoImpl implements StudentDao {
- 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 StudentManager.java中的完整代码为:
- package com.demo.service;
- import java.util.List;
- import com.demo.model.Student;
- public interface StudentManager {
- public List<Student> getAllStudents();
- }
10 StudentManagerImpl.java中的完整代码为:
- package com.demo.service;
- import java.util.List;
- import org.springframework.stereotype.Service;
- import org.springframework.beans.factory.annotation.Autowired;
- import com.demo.dao.StudentDao;
- import com.demo.model.Student;
- import com.demo.service.StudentManager;
- @Service
- public class StudentManagerImplimplements StudentManager{
- @Autowired
- StudentDao dao;
- publicList<Student> getAllStudents() {
- return dao.getAllStudents();
- }
- }
11 StudentController.java中的完整代码为:
- package com.demo.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.ui.Model;
- import com.demo.service.StudentManager;
- @Controller
- @RequestMapping("/student-module")
- public class StudentController {
- @Autowired
- StudentManager manager;
- @RequestMapping(value = "/getStudentInfo", method = RequestMethod.GET)
- public String getStudentInfo(Model model) {
- model.addAttribute("students", manager.getAllStudents());
- return "showStudentInfo";
- }
- }
12 showStudentInfo.jsp中的完整代码为:
- <%@ 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 将springmvcdemo添加进Tomcat7中并运行
在浏览器中输入
http://localhost:8080/springmvcdemo/student-module/getStudentInfo
显示结果为
14 源码下载地址
CSDN:http://download.csdn.net/detail/haishu_zheng/9531315
学习时的痛苦是暂时的 未学到的痛苦是终生的