您的位置:首页 > 编程语言 > Java开发

SpringMVC学习(二)

2016-07-19 21:46 531 查看

SpringMVC入门(注解方式)

需求

实现商品查询列表功能。

第一步:创建Web项目

springmvc02

第二步:导入jar包

第三步:配置前端控制器

在WEB-INF\web.xml中配置前端控制器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>springmvc02</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载SpringMVC配置文件 -->
<init-param>
<!-- 如果不配置contextConfigLocation,默认查找的配置文件名称classpath下的:servlet名称+"-serlvet.xml"即:springmvc-serlvet.xml-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
可以配置/ ,此工程所有请求全部由springmvc解析,此种方式可以实现 RESTful方式,需要特殊处理对静态文件的解析不能由springmvc解析
可以配置*.do或*.action,所有请求的url扩展名为.do或.action由springmvc解析,此种方法常用
不可以/*,如果配置/*,返回jsp也由springmvc解析,这是不对的。
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>

</web-app>

 

第四步:springmvc配置文件

Springmvc默认加载WEB-INF/[前端控制器的名字]-servlet.xml,也可以在前端控制器定义处指定加载的配置文件,如下:

<!-- 加载SpringMVC配置文件 -->
<init-param>
<!-- 如果不配置contextConfigLocation,默认查找的配置文件名称classpath下的:servlet名称+"-serlvet.xml"即:springmvc-serlvet.xml-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>

第五步:配置组件扫描器

在springmvc.xml文件配置如下:

<!-- 使用spring组件扫描 -->
<context:component-scan base-package="com.cxz.springmvc.first" />

使用组件扫描器省去在spring容器配置每个controller类的繁琐。使用<context:component-scan自动扫描标记@controller的控制器类。

建议使用组件扫描,组件扫描可以扫描@Controller、@Service、@component、@Repsitory

第六步:配置注解处理器映射器

在springmvc.xml文件配置如下:

<!-- 注解处理器映射器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</bean>

注解式处理器映射器,对类中标记@ResquestMapping的方法进行映射,根据ResquestMapping定义的url匹配ResquestMapping标记的方法,匹配成功返回HandlerMethod对象给前端控制器,HandlerMethod对象中封装url对应的方法Method。

第七步:配置注解处理器适配器

<!-- 注解处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />

注解式处理器适配器,对标记@ResquestMapping的方法进行适配。

第八步:配置mvc:annotation-driven

<!-- 通过annotation-driven可以替代上边的处理器映射器和适配器 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>

通过annotation-driven可以替代第六步和第七步。

第九步:配置视图解析器

在springmvc.xml文件配置如下:

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

InternalResourceViewResolver:支持JSP视图解析;

viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,所以classpath中必须包含jstl的相关jar 包;

prefix 和suffix:查找视图页面的前缀和后缀,最终视图的址为:

前缀+逻辑视图名+后缀,逻辑视图名需要在controller中返回ModelAndView指定,比如逻辑视图名为hello,则最终返回的jsp视图地址 “WEB-INF/jsp/hello.jsp”

第十步:后端控制器开发

com.cxz.springmvc.po添加po类

package com.cxz.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();
}
}

com.cxz.springmvc.first添加Controller

@Controller
public class ItemController {
@RequestMapping("/queryItems")
public ModelAndView queryItems() {
// 使用静态数据将商品信息列表显示在jsp页面
// 商品列表
List<Items> itemsList = new ArrayList<Items>();

Items items_1 = new Items();
items_1.setName("商品1");
items_1.setPrice(6000f);
items_1.setCreatetime(new Date());
items_1.setDetail("这是商品1");

Items items_2 = new Items();
items_2.setName("商品2");
items_2.setPrice(5000f);
items_2.setDetail("这是商品2");

itemsList.add(items_1);
itemsList.add(items_2);

ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList", itemsList);
//指定逻辑视图名
modelAndView.setViewName("itemsList");

return modelAndView;
}

第十一步:视图开发

创建/WEB-INF/jsp/itemsList.jsp视图页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
</head>
<body>
商品列表:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>商品描述</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td>${item.detail }</td>
</tr>
</c:forEach>

</table>

</body>
</html>

第十二步:部署在tomcat测试

http://localhost:8080/springmvc02/queryItems.action

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: