admin管理员组文章数量:1794759
史上最简单的 Spring MVC 教程(五)
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. */ @Service public 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. */ @Controller public 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="java.sun/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="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:mvc="www.springframework/schema/mvc" xmlns:context="www.springframework/schema/context" xmlns:aop="www.springframework/schema/aop" xmlns:tx="www.springframework/schema/tx" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-3.2.xsd www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-3.2.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-3.2.xsd www.springframework/schema/aop www.springframework/schema/aop/spring-aop-3.2.xsd www.springframework/schema/tx www.springframework/schema/tx/spring-tx-3.2.xsd www.springframework/schema/tx www.springframework/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="java.sun/xml/ns/javaee" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="java.sun/xml/ns/javaee java.sun/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 浏览器中访问 localhost:8080/springmvc-annotation/person/all.action,编译及运行成功后,将会返回如下结果页面:
———— ☆☆☆ —— 返回 -> 史上最简单的 Spring MVC 教程 <- 目录 —— ☆☆☆ ————
版权声明:本文标题:史上最简单的 Spring MVC 教程(五) 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686617098a86704.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论