Spring MVC入门Demo

1 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为springmvcdemo的Maven工程。

 

2 文件目录结构如下图所示

 

3 pom.xml中的完整内容为

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>springmvcdemo</groupId>  
  5.     <artifactId>springmvcdemo</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.     <packaging>war</packaging>  
  8.     <name>springmvcdemo</name>  
  9.     <description />  
  10.     <properties>  
  11.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  12.     </properties>  
  13.     <dependencies>  
  14.         <dependency>  
  15.             <groupId>org.springframework</groupId>  
  16.             <artifactId>spring-web</artifactId>  
  17.             <version>3.2.9.RELEASE</version>  
  18.         </dependency>  
  19.         <dependency>  
  20.             <groupId>org.springframework</groupId>  
  21.             <artifactId>spring-webmvc</artifactId>  
  22.             <version>3.2.9.RELEASE</version>  
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>javax</groupId>  
  26.             <artifactId>javaee-api</artifactId>  
  27.             <version>7.0</version>  
  28.             <scope>provided</scope>  
  29.         </dependency>  
  30.         <dependency>  
  31.             <groupId>org.glassfish.web</groupId>  
  32.             <artifactId>javax.servlet.jsp.jstl</artifactId>  
  33.             <version>1.2.2</version>  
  34.         </dependency>   
  35.     </dependencies>  
  36.     <build>  
  37.         <plugins>  
  38.             <plugin>  
  39.                 <artifactId>maven-compiler-plugin</artifactId>  
  40.                 <version>2.3.2</version>  
  41.                 <configuration>  
  42.                     <source>1.7</source>  
  43.                     <target>1.7</target>  
  44.                 </configuration>  
  45.             </plugin>  
  46.             <plugin>  
  47.                 <artifactId>maven-war-plugin</artifactId>  
  48.                 <version>2.2</version>  
  49.                 <configuration>  
  50.                     <version>3.1</version>  
  51.                     <failOnMissingWebXml>false</failOnMissingWebXml>  
  52.                 </configuration>  
  53.             </plugin>  
  54.         </plugins>  
  55.     </build>  
  56. </project>  

 

 

 

4 web.xml中的完整内容为

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://xmlns.jcp.org/xml/ns/javaee"  
  4.     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"  
  5.     id="WebApp_ID" version="3.1">  
  6.     <display-name>springmvcdemo</display-name>  
  7.     <servlet>  
  8.         <servlet-name>spring</servlet-name>  
  9.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  10.         <init-param>  
  11.             <param-name>contextConfigLocation</param-name>  
  12.             <param-value>/WEB-INF/applicationContext.xml</param-value>  
  13.         </init-param>  
  14.         <load-on-startup>1</load-on-startup>  
  15.     </servlet>  
  16.   
  17.     <servlet-mapping>  
  18.         <servlet-name>spring</servlet-name>  
  19.         <url-pattern>/</url-pattern>  
  20.     </servlet-mapping>  
  21. </web-app>  

 

5 applicationContext.xml中的完整内容为:

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  6.         http://www.springframework.org/schema/context  
  7.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  8.    
  9.     <context:component-scan base-package="com.demo" />  
  10.    
  11.     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
  12.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
  13.        
  14.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  15.         <property name="prefix" value="/WEB-INF/views/" />  
  16.         <property name="suffix" value=".jsp" />  
  17.     </bean>  
  18.    
  19. </beans>  

 

 

6 Student.Java中的完整代码为

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.demo.model;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Student implements Serializable {  
  6.     private static final long serialVersionUID = 1L;  
  7.        
  8.     private Integer id;  
  9.     private String name;  
  10.    
  11.     public Integer getId() {  
  12.         return id;  
  13.     }  
  14.   
  15.     public void setId(Integer id) {  
  16.         this.id = id;  
  17.     }  
  18.   
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.   
  23.     public void setName(String name) {  
  24.         this.name = name;  
  25.     }  
  26.   
  27.     @Override  
  28.     public String toString() {  
  29.         return "Student [id=" + id + ", name=" + name + "]";  
  30.     }  
  31. }  

 

 

7 StudentDao.java中的完整代码为

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.demo.dao;  
  2.    
  3. import java.util.List;  
  4. import com.demo.model.Student;  
  5.    
  6. public interface StudentDao {  
  7.     public List<Student> getAllStudents();  
  8. }  

 

8 StudentDaoImpl.java中的完整代码为

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.demo.dao;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.springframework.stereotype.Repository;  
  7.   
  8. import com.demo.model.Student;  
  9.   
  10. @Repository  
  11. public class StudentDaoImpl implements StudentDao {  
  12.     public List<Student> getAllStudents() {  
  13.         List<Student> students = new ArrayList<Student>();  
  14.            
  15.         Student stu1 = new Student();  
  16.         stu1.setId(1);  
  17.         stu1.setName("Zhang San");  
  18.         students.add(stu1);  
  19.            
  20.         Student stu2 = new Student();  
  21.         stu2.setId(2);  
  22.         stu2.setName("Li Si");  
  23.         students.add(stu2);  
  24.            
  25.         return students;  
  26.     }  
  27. }  

 

 

9 StudentManager.java中的完整代码为:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.demo.service;  
  2.    
  3. import java.util.List;  
  4.    
  5. import com.demo.model.Student;  
  6.    
  7. public interface StudentManager {  
  8.     public List<Student> getAllStudents();  
  9. }  

 

10 StudentManagerImpl.java中的完整代码为:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.demo.service;  
  2.    
  3. import java.util.List;  
  4.    
  5. import org.springframework.stereotype.Service;  
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7.    
  8. import com.demo.dao.StudentDao;  
  9. import com.demo.model.Student;  
  10. import com.demo.service.StudentManager;  
  11.    
  12. @Service  
  13. public class StudentManagerImplimplements StudentManager{  
  14.     @Autowired  
  15.     StudentDao dao;  
  16.       
  17.     publicList<Student> getAllStudents() {  
  18.         return dao.getAllStudents();  
  19.     }  
  20. }  

 

11 StudentController.java中的完整代码为:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.demo.controller;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import org.springframework.web.bind.annotation.RequestMethod;  
  7. import org.springframework.ui.Model;  
  8.   
  9. import com.demo.service.StudentManager;  
  10.   
  11. @Controller  
  12. @RequestMapping("/student-module")  
  13.   
  14. public class StudentController {  
  15.     @Autowired  
  16.     StudentManager manager;  
  17.    
  18.     @RequestMapping(value = "/getStudentInfo", method = RequestMethod.GET)  
  19.     public String getStudentInfo(Model model) {  
  20.         model.addAttribute("students", manager.getAllStudents());  
  21.         return "showStudentInfo";  
  22.     }  
  23. }  

 

 

12 showStudentInfo.jsp中的完整代码为:

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  2. <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>  
  3. <html>  
  4. <head>  
  5.     <title>Spring MVC Demo</title>  
  6. </head>  
  7.    
  8. <body>  
  9.     <h2>All Students</h2>  
  10.    
  11.     <table border="1">  
  12.         <tr>  
  13.             <th>Student Id</th>  
  14.             <th>Student Name</th>  
  15.         </tr>  
  16.         <c:forEach items="${students}" var="student">  
  17.             <tr>  
  18.                 <td>${student.id}</td>  
  19.                 <td>${student.name}</td>  
  20.             </tr>  
  21.         </c:forEach>  
  22.     </table>  
  23.    
  24. </body>  
  25. </html>  

 

 

13 将springmvcdemo添加进Tomcat7中并运行

 

在浏览器中输入

http://localhost:8080/springmvcdemo/student-module/getStudentInfo

 

显示结果为

 

14 源码下载地址

 

CSDN:http://download.csdn.net/detail/haishu_zheng/9531315

 

Github:https://github.com/zhenghaishu/SpringMVC-Demo

posted @ 2017-04-19 11:56  爱你爱自己  阅读(251)  评论(0编辑  收藏  举报