非注解方式
项目结构图
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--SpringMVC前端控制器配置-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
dispatcher-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置Heandler-->
<bean name="/queryItems.action" class="ssm.controller.ItemsController1"/>
<!--处理器适配器-->
<!--所有的处理器适配器都实现了HandlerAdapter接口-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--处理器映射器-->
<!--将Bean的name作为URL进行查找,需要在配置handler时指定beanname(就是url)-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--视图解析器-->
<!--配置解析jsp的视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
ItemsController1.java
package ssm.controller;
/**
* Created with IntelliJ IDEA.
* User: YEN
* Date: 2016/8/6
* Time: 10:40
*/
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import ssm.pojo.Items;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class ItemsController1 implements Controller{
@Override
public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
//调用service查找数据库,查询商品列表 这里使用静态数据模拟
List<Items> itemsList=new ArrayList<Items>();
//向list中填充静态数据
Items items_1 = new Items();
items_1.setName("联想笔记本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 联想笔记本电脑!");
Items items_2 = new Items();
items_2.setName("苹果手机");
items_2.setPrice(5000f);
items_2.setDetail("iphone6苹果手机!");
itemsList.add(items_1);
itemsList.add(items_2);
//返回ModelAndView
ModelAndView modelAndView=new ModelAndView();
//相当于request.setAttribut 在jsp中通过itemsList取数据
modelAndView.addObject("itemsList",itemsList);
//指定视图
modelAndView.setViewName("items/itemsList.jsp");
return modelAndView;
}
}
items.java
package ssm.pojo;
import java.util.Date;
/**
* Created with IntelliJ IDEA.
* User: YEN
* Date: 2016/8/6
* Time: 10:49
*/
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;
}
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;
}
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());
}
}
itemsList.jsp
<%--
Created by IntelliJ IDEA.
User: YEN
Date: 2016/8/6
Time: 11:00
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
<head>
<title>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/queryItems.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.contextPath }/editItem.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
注解方式
dispatcher-servlet.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/tool"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--加载注解的Heandler-->
<!--实际开发中建议组件扫描-->
<context:component-scan base-package="ssm.controller"> </context:component-scan>
<!--mvc:annotation-driven默认加载很多的参数绑定方法-->
<!--<mvc:annotation></mvc:annotation>-->
<!--注解适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!--注解映射器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!--视图解析器-->
<!--配置解析jsp的视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
ItemsController3.java
package ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import ssm.pojo.Items;
import java.util.ArrayList;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: YEN
* Date: 2016/8/6
* Time: 13:28
*/
//标记它是一个注解 @Controller标识
@Controller
public class ItemsController3 {
//商品查询列表
// @RenderMapping("/queryItems")实现的是方法和URL的映射
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception{
//调用service查找数据库,查询商品列表 这里使用静态数据模拟
List<Items> itemsList=new ArrayList<Items>();
//向list中填充静态数据
Items items_1 = new Items();
items_1.setName("联想电脑");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 联想笔记本电脑!");
Items items_2 = new Items();
items_2.setName("苹果手机");
items_2.setPrice(5000f);
items_2.setDetail("iphone6苹果手机!");
itemsList.add(items_1);
itemsList.add(items_2);
//返回ModelAndView
ModelAndView modelAndView=new ModelAndView();
//相当于request.setAttribut 在jsp中通过itemsList取数据
modelAndView.addObject("itemsList",itemsList);
//指定视图
modelAndView.setViewName("items/itemsList.jsp");
return modelAndView;
}
}