springmvc注解映射器和适配器配置
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" />
还没有评论,来说两句吧...