springmvc注解映射器和适配器配置
来源:互联网 发布:抽查天猫京东淘宝假货 编辑:程序博客网 时间:2024/04/27 08:24
springmvc注解映射器和适配器配置
环境搭建
请参考:http://blog.csdn.net/leisure_life/article/details/72832698
创建bean
Items .java
package com.ld.springmvc.po;import java.util.Date;public class Items { private Integer id; private String name; private Float price; private String pic; private Date createtime; private String detail; 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 == null ? null : name.trim(); } public Float getPrice() { return price; } public void setPrice(Float price) { this.price = price; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic == null ? null : pic.trim(); } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail == null ? null : detail.trim(); }}
创建spring配置文件
spring.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: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"> </beans>
配置前端控制器
在web.xml中配置
<?xml version="1.0" encoding="UTF-8"?><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"> <!-- springmvc前端控制器 --> <servlet> <servlet-name>beautiful</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置springmvc加载的配置文件(配置处理器映射器、适配器等) --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>beautiful</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping></web-app>
编写处理器
ItemsController.java
package com.ld.springmvc.controller;/** * Handler */import java.util.ArrayList;import java.util.List;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.ld.springmvc.po.Items;public class ItemsController{ //编写商品查询方法 public ModelAndView findItemsList()throws Exception{ List<Items> itemsList = new ArrayList<Items>(); Items item = new Items(); item.setName("旺仔"); item.setPrice(10f); Items item1 = new Items(); item1.setName("旺1仔"); item1.setPrice(11f); Items item2 = new Items(); item2.setName("旺仔"); item2.setPrice(10f); Items item3 = new Items(); item3.setName("旺2仔"); item3.setPrice(10f); itemsList.add(item); itemsList.add(item1); itemsList.add(item2); itemsList.add(item3); ModelAndView modelAndView = new ModelAndView(); //类似于request的setAttribute modelAndView.addObject("itemsList", itemsList); //指定视图 modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp"); return modelAndView; }}
配置处理器映射器
spring.xml中配置
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
配置处理器适配器
spring.xml中配置
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
处理器添加注解
package com.ld.springmvc.controller;/** * 注解的Handler */import java.util.ArrayList;import java.util.List;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.ld.springmvc.po.Items;@Controller //标识它是一个处理器public class ItemsController{ //编写商品查询方法 @RequestMapping("/findItemsList.action")//建议将url与方法名一致。实现的是方法findItemsList与url进行映射 public ModelAndView findItemsList()throws Exception{ List<Items> itemsList = new ArrayList<Items>(); Items item = new Items(); item.setName("旺仔"); item.setPrice(10f); Items item1 = new Items(); item1.setName("旺1仔"); item1.setPrice(11f); Items item2 = new Items(); item2.setName("旺仔"); item2.setPrice(10f); Items item3 = new Items(); item3.setName("旺2仔"); item3.setPrice(10f); itemsList.add(item); itemsList.add(item1); itemsList.add(item2); itemsList.add(item3); ModelAndView modelAndView = new ModelAndView(); //类似于request的setAttribute modelAndView.addObject("itemsList", itemsList); //指定视图 modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp"); return modelAndView; }}
配置处理器
在spring.xml中使用组件扫描器
<!-- 对于注解的Handler可以单个配置 实际开发中建议使用组件扫描 --> <context:component-scan base-package="com.ld.springmvc.controller"/>
编写前端页面
itemsList.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%response.setContentType("text/html;charset = GB2312");request.setCharacterEncoding("gb2312");%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>查询商品列表</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="${pageContext.request.contextPath }/item/queryItem.action" method="post"> 查询条件 <table width="100%" border="1"> <tr> <td><input type="submit" value="查询" /></td> </tr> </table> 商品列表: <table width="100%" border="1"> <tr> <td>商品名称</td> <td>商品价格</td> <td>生产日期</td> <td>商品描述</td> <td>操作</td> </tr> <c:forEach items="${itemsList }" var="item"> <tr> <td>${item.name }</td> <td>${item.price }</td> <td><fmt:formatDate value="${item.createtime }" pattern="yyyy-MM-dd HH:mm:ss" /></td> <td>${item.detail }</td> <td><a href="${pageContext.request.contentType }/item/editItem.action?id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </form> </body></html>
配置视图解析器
在spring.xml中配置视图解析器
<!-- 视图解析器 配置解析jsp的视图解析器,默认使用jstl标签 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
测试
测试路径:http://127.0.0.1/springmvc_annotation/findItemsList.action
测试结果
阅读全文
0 0
- springmvc注解映射器和适配器配置
- 【SpringMVC框架】注解的处理器映射器和适配器配置
- springmvc 注解的适配器和映射器的配置
- 【SpringMVC框架】注解的处理器映射器和适配器配置
- SpringMVC - 注解的适配器和映射器配置
- springmvc 注解的适配器和映射器的配置
- SpringMVC注解的映射器和适配器
- SpringMVC非注解的处理器的映射器和适配器的配置
- 【SpringMVC框架】非注解的处理器映射器和适配器
- springmvc(三)注解的处理器映射器和适配器
- 【SSM-SpringMVC框架】注解的处理器适配器和映射器
- 【SpringMVC框架】非注解的处理器映射器和适配器
- SpringMVC - 非注解的处理器映射器和适配器
- SpringMVC非注解的处理映射器和适配器
- springMVC的前端控制器、注解适配器和映射器
- springmvc非注解的处理器映射器和适配器
- springmvc注解的处理器映射器和适配器
- SpringMVC学习之注解映射器和适配器
- Android--广播接收者
- 左侧固定菜单栏,页面滚动定位(菜单栏定位)
- 字节流与字符流的区别
- 一篇文了解DevOps:从概念、关键问题、兴起到实现需求
- 名企笔试:美团2016招聘笔试(二维数组打印)
- springmvc注解映射器和适配器配置
- sql 面试和优化
- JavaScript bind方法
- 有时候,一点小问题,就会纠结你半天,真是基础不牢啊。 彻底解决 django的模板templates路径的问题; 一般会提示什么模板找不到,类似:TemplateDoesNotExist at /ac
- Unity A*寻路三个简单实用的算法
- 当前网页关闭事件拦截onbeforeunload ,visibilitychange
- 八大排序算法Java
- pytorch-custom dataset
- sigmoid和softmax总结