1 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为springmvcdemo的Maven工程。
2 文件目录结构如下图所示
3 pom.xml中的完整内容为
4.0.0
springmvcdemo
springmvcdemo
0.0.1-SNAPSHOT
war
springmvcdemo
UTF-8
org.springframework
spring-web
3.2.9.RELEASE
org.springframework
spring-webmvc
3.2.9.RELEASE
javax
javaee-api
7.0
provided
org.glassfish.web
javax.servlet.jsp.jstl
1.2.2
maven-compiler-plugin
2.3.2
1.7
maven-war-plugin
2.2
3.1
false
4 web.xml中的完整内容为
springmvcdemo
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/applicationContext.xml
1
spring
/
5 applicationContext.xml中的完整内容为:
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 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 getAllStudents() {
List students = new ArrayList();
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 getAllStudents();
}
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 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"%>
Spring MVC Demo
All Students
Student Id
Student Name
${student.id}
${student.name}
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