springmvc注解映射器和适配器配置

矫情吗;* 2022-06-15 11:57 284阅读 0赞

springmvc注解映射器和适配器配置

环境搭建

请参考:http://blog.csdn.net/leisure_life/article/details/72832698

创建bean

Items .java

  1. package com.ld.springmvc.po;
  2. import java.util.Date;
  3. public class Items {
  4. private Integer id;
  5. private String name;
  6. private Float price;
  7. private String pic;
  8. private Date createtime;
  9. private String detail;
  10. public Integer getId() {
  11. return id;
  12. }
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name == null ? null : name.trim();
  21. }
  22. public Float getPrice() {
  23. return price;
  24. }
  25. public void setPrice(Float price) {
  26. this.price = price;
  27. }
  28. public String getPic() {
  29. return pic;
  30. }
  31. public void setPic(String pic) {
  32. this.pic = pic == null ? null : pic.trim();
  33. }
  34. public Date getCreatetime() {
  35. return createtime;
  36. }
  37. public void setCreatetime(Date createtime) {
  38. this.createtime = createtime;
  39. }
  40. public String getDetail() {
  41. return detail;
  42. }
  43. public void setDetail(String detail) {
  44. this.detail = detail == null ? null : detail.trim();
  45. }
  46. }

创建spring配置文件

spring.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  3. </beans>

配置前端控制器

在web.xml中配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="3.0" 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_3_0.xsd">
  3. <!-- springmvc前端控制器 -->
  4. <servlet>
  5. <servlet-name>beautiful</servlet-name>
  6. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  7. <!-- 配置springmvc加载的配置文件(配置处理器映射器、适配器等) -->
  8. <init-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath:springmvc.xml</param-value>
  11. </init-param>
  12. </servlet>
  13. <servlet-mapping>
  14. <servlet-name>beautiful</servlet-name>
  15. <url-pattern>*.action</url-pattern>
  16. </servlet-mapping>
  17. </web-app>

编写处理器

ItemsController.java

  1. package com.ld.springmvc.controller;
  2. /** * Handler */
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.servlet.ModelAndView;
  7. import com.ld.springmvc.po.Items;
  8. public class ItemsController{
  9. //编写商品查询方法
  10. public ModelAndView findItemsList()throws Exception{
  11. List<Items> itemsList = new ArrayList<Items>();
  12. Items item = new Items();
  13. item.setName("旺仔");
  14. item.setPrice(10f);
  15. Items item1 = new Items();
  16. item1.setName("旺1仔");
  17. item1.setPrice(11f);
  18. Items item2 = new Items();
  19. item2.setName("旺仔");
  20. item2.setPrice(10f);
  21. Items item3 = new Items();
  22. item3.setName("旺2仔");
  23. item3.setPrice(10f);
  24. itemsList.add(item);
  25. itemsList.add(item1);
  26. itemsList.add(item2);
  27. itemsList.add(item3);
  28. ModelAndView modelAndView = new ModelAndView();
  29. //类似于request的setAttribute
  30. modelAndView.addObject("itemsList", itemsList);
  31. //指定视图
  32. modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
  33. return modelAndView;
  34. }
  35. }

配置处理器映射器

spring.xml中配置

  1. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

配置处理器适配器

spring.xml中配置

  1. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

处理器添加注解

  1. package com.ld.springmvc.controller;
  2. /** * 注解的Handler */
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.servlet.ModelAndView;
  8. import com.ld.springmvc.po.Items;
  9. @Controller //标识它是一个处理器
  10. public class ItemsController{
  11. //编写商品查询方法
  12. @RequestMapping("/findItemsList.action")//建议将url与方法名一致。实现的是方法findItemsList与url进行映射
  13. public ModelAndView findItemsList()throws Exception{
  14. List<Items> itemsList = new ArrayList<Items>();
  15. Items item = new Items();
  16. item.setName("旺仔");
  17. item.setPrice(10f);
  18. Items item1 = new Items();
  19. item1.setName("旺1仔");
  20. item1.setPrice(11f);
  21. Items item2 = new Items();
  22. item2.setName("旺仔");
  23. item2.setPrice(10f);
  24. Items item3 = new Items();
  25. item3.setName("旺2仔");
  26. item3.setPrice(10f);
  27. itemsList.add(item);
  28. itemsList.add(item1);
  29. itemsList.add(item2);
  30. itemsList.add(item3);
  31. ModelAndView modelAndView = new ModelAndView();
  32. //类似于request的setAttribute
  33. modelAndView.addObject("itemsList", itemsList);
  34. //指定视图
  35. modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
  36. return modelAndView;
  37. }
  38. }

配置处理器

在spring.xml中使用组件扫描器

  1. <!-- 对于注解的Handler可以单个配置 实际开发中建议使用组件扫描 -->
  2. <context:component-scan base-package="com.ld.springmvc.controller"/>

编写前端页面

itemsList.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <% response.setContentType("text/html;charset = GB2312"); request.setCharacterEncoding("gb2312"); %>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  4. <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
  5. <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>查询商品列表</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
  17. </head>
  18. <body>
  19. <form action="${pageContext.request.contextPath }/item/queryItem.action" method="post">
  20. 查询条件
  21. <table width="100%" border="1">
  22. <tr>
  23. <td><input type="submit" value="查询" /></td>
  24. </tr>
  25. </table>
  26. 商品列表:
  27. <table width="100%" border="1">
  28. <tr>
  29. <td>商品名称</td>
  30. <td>商品价格</td>
  31. <td>生产日期</td>
  32. <td>商品描述</td>
  33. <td>操作</td>
  34. </tr>
  35. <c:forEach items="${itemsList }" var="item">
  36. <tr>
  37. <td>${item.name }</td>
  38. <td>${item.price }</td>
  39. <td><fmt:formatDate value="${item.createtime }" pattern="yyyy-MM-dd HH:mm:ss" /></td>
  40. <td>${item.detail }</td>
  41. <td><a href="${pageContext.request.contentType }/item/editItem.action?id=${item.id}">修改</a></td>
  42. </tr>
  43. </c:forEach>
  44. </table>
  45. </form>
  46. </body>
  47. </html>

配置视图解析器

在spring.xml中配置视图解析器

  1. <!-- 视图解析器 配置解析jsp的视图解析器,默认使用jstl标签 -->
  2. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />

测试

测试路径:http://127.0.0.1/springmvc_annotation/findItemsList.action

" class="reference-link">测试结果 这里写图片描述

发表评论

表情:
评论列表 (有 0 条评论,284人围观)

还没有评论,来说两句吧...

相关阅读