黑马程序员技术交流社区

标题: 【郑州校区】 Java之springmvc-sublun-day01(上) [打印本页]

作者: 我是色色    时间: 2018-2-2 10:28
标题: 【郑州校区】 Java之springmvc-sublun-day01(上)
本帖最后由 我是色色 于 2018-2-2 10:43 编辑

1、 课程计划
第一天
1、Springmvc介绍
2、入门程序
3、Springmvc架构讲解
a) 框架结构
b) 组件说明
4、Springmvc整合mybatis
5、参数绑定
a) Springmvc默认支持的类型
b) 简单数据类型
c) Pojo类型
d) Pojo包装类型
e) 自定义参数绑定
6、Springmvc和struts2的区别
第二天
1、高级参数绑定
a) 数组类型的参数绑定
b) List类型的绑定
2、@RequestMapping注解的使用
3、Controller方法返回值
4、Springmvc中异常处理
5、图片上传处理
6、Json数据交互
7、Springmvc实现Restful
8、拦截器
2 Spring web mvc介绍2.1 Springmvc是什么?
Spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来:
2.2 SpringMVC处理流程


3 入门程序3.1 开发环境
本教程使用环境:
Jdk:jdk1.7.0_72
Eclipse:mars
Tomcat:apache-tomcat-7.0.53
Springmvc:4.1.3
3.2 需求
使用springmvc实现商品列表的展示。
3.3 需求分析
请求的url:/itemList.action
参数:无
数据:静态数据
3.4 开发步骤3.4.1 第一步:创建一个javaweb工程3.4.2 第二步:导入jar包
3.4.3 第三步:创建itemList.jsp
把参考资料中的itemList.jsp复制到工程的/WEB-INF/jsp目录下。
3.4.4 第四步:创建ItemsController
ItemController是一个普通的java类,不需要实现任何接口,只需要在类上添加@Controller注解即可。@RequestMapping注解指定请求的url,其中“.action”可以加也可以不加。在ModelAndView对象中,将视图设置为“/WEB-INF/jsp/itemList.jsp”
[AppleScript] 纯文本查看 复制代码
@Controller

public class ItemController {



@RequestMapping("/itemList")

public ModelAndView itemList() throws Exception {


List<Items> itemList = new ArrayList<>();


//商品列表

Items items_1 = new Items();

items_1.setName("联想笔记本_3");

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苹果手机!");


itemList.add(items_1);

itemList.add(items_2);

//创建modelandView对象

ModelAndView modelAndView = new ModelAndView();

//添加model

modelAndView.addObject("itemList", itemList);

//添加视图

modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");

//modelAndView.setViewName("itemsList");

return modelAndView;

}



}

商品数据使用Items类描述,可以使用参考资料中提供的pojo类,

3.4.5 第五步:创建springmvc.xml
[AppleScript] 纯文本查看 复制代码
<?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:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd

        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd">

        

<context:component-scan base-package="cn.itcast.springmvc.controller"/>


</beans>

3.4.6 第六步:配置前端控制器
在web.xml中添加DispatcherServlet的配置。

[AppleScript] 纯文本查看 复制代码
[mw_shl_code=applescript,true]<!-- 前端控制器 -->

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:springmvc.xml</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>*.action</url-pattern>

</servlet-mapping>


[/mw_shl_code]




作者: 我是色色    时间: 2018-2-2 10:28





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2