史上最简单的 Spring MVC 教程(五)
来源:互联网 发布:书店管理系统完整源码 编辑:程序博客网 时间:2024/05/20 12:48
1 前言
在史上最简单的 Spring MVC 教程(四)中,咱们已经感受到了 Spring MVC 框架的注解的方便之处啦!那么,接下来,就让咱们进一步体验注解的魅力,用注解的方式实现显示“人员列表”的功能。
2 注解示例 - 显示人员列表
在本部分,咱们的目的就是实现在页面上显示“人员列表”的功能,但由于咱们没有连接数据库,所以咱们可以在 service 层模拟内存数据库,然后通过注解的方式注入到 Controller 中,接下来,还需要新建 JSP 页面,创建用于配置自动扫描 service 层的 beans.xml 文件,以及修改 web.xml 文件等。在此,首先给出项目结构图,如下所示:
第一步:模拟 Service 层(PersonService)
package spring.mvc.service;import org.springframework.stereotype.Service;import spring.mvc.domain.Person;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * Created by 维C果糖 on 2017/1/26. */@Servicepublic class PersonService { // 模拟内存数据库,准备数据 // 声明一个容器 private static Map<Integer, Person> map = new HashMap<Integer, Person>(); // 利用静态块初始化数据 static { for (int i = 0; i < 10; i++) { Person p = new Person(); p.setId(i); p.setName("Charie"+i); p.setAge(10+i); map.put(i,p); } } // 获取人员列表 public List<Person> findAll(){ // 将 map 对象转换为 list 结合 return new ArrayList(map.values()); }}
第二步:创建控制器(PersonController)
package spring.mvc.controller;import com.sun.glass.ui.mac.MacPasteboard;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import spring.mvc.domain.Person;import spring.mvc.service.PersonService;import javax.annotation.Resource;import java.util.List;import java.util.Map;/** * Created by 维C果糖 on 2017/1/26. */@Controllerpublic class PersonController { @Resource PersonService ps; // 注入 service 层 @RequestMapping(value = "/person/all") public String findAll(Map<String,Object> model){ // 声明 model 用来传递数据 List<Person> personList = ps.findAll(); model.put("personList",personList); // 通过这一步,JSP 页面就可以访问 personList return "/person/jPersonList"; // 跳转到 jPersonList 页面 }}
第三步:新建 JSP 页面(jPersonList)
<%-- Created by IntelliJ IDEA. User: 维C果糖 Date: 2017/1/27 Time: 00:00 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><html><head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>PersonList</title></head><body> <form action="${pageContext.request.contextPath}/personform.action" method="post"> <div style="padding:20px;"> 人员列表 </div> <table border="1"> <tr> <td>编号:</td> <td>姓名:</td> <td>年龄:</td> </tr> <c:forEach items="${personList}" var="p"> <tr> <td>${p.id}</td> <td>${p.name}</td> <td>${p.age}</td> </tr> </c:forEach> </table> </form></body></html>
第四步:新建 beans.xml 配置文件
<?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: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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx "> <!-- 组件扫描,扫描 service层 --> <context:component-scan base-package="spring.mvc.service"/></beans>
第五步:修改 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"> <!-- 配置 Spring 框架 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置 DispatcherServlet,对所有后缀为action的url进行过滤 --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 修改 Spring MVC 配置文件的位置和名称 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
在完成以上操作后,启动 tomcat 服务器,在 Chrome 浏览器中访问 http://localhost:8080/springmvc-annotation/person/all.action,编译及运行成功后,将会返回如下结果页面:
———— ☆☆☆ —— 返回 -> 史上最简单的 Spring MVC 教程 <- 目录 —— ☆☆☆ ————
2 0
- 史上最简单的 Spring MVC 教程(五)
- 史上最简单的 Spring MVC 教程(五)
- 史上最简单的 Spring MVC 教程
- 史上最简单的 Spring MVC 教程
- 史上最简单的 Spring MVC 教程(一)
- 史上最简单的 Spring MVC 教程(二)
- 史上最简单的 Spring MVC 教程(三)
- 史上最简单的 Spring MVC 教程(四)
- 史上最简单的 Spring MVC 教程(六)
- 史上最简单的 Spring MVC 教程(七)
- 史上最简单的 Spring MVC 教程(八)
- 史上最简单的 Spring MVC 教程(九)
- 史上最简单的 Spring MVC 教程(十)
- 史上最简单的 Spring MVC 教程(一)
- 史上最简单的 Spring MVC 教程(二)
- 史上最简单的 Spring MVC 教程(三)
- 史上最简单的 Spring MVC 教程(四)
- 史上最简单的 Spring MVC 教程(六)
- PHP 代码复用trait
- 多个同1div float left换行题
- Android开发系列——listfragment的使用例子
- detailsview控件 为过程或函数 UpdateVisit1 了过多的参数 题
- 菜鸟:《面试流程概要》从简历到笔试
- 史上最简单的 Spring MVC 教程(五)
- 《跟任何人都聊得来》阅读笔记
- spring+springMVC+myBatis——小案例
- eclipse调试app无法进入断点
- 安卓23x 阿拉伯语显示连写java patch
- ActiveX插件在网页上显示题
- 1个关于button移动
- android 监听后台到前台的切换
- Ubuntu安装VPN