资料来自:www.sikiedu.com
一、SpringMVC
SpringMVC又叫SpringWebMVC是一个轻量级的基于请求响应的表现层框架。它是Spring框架的一部分。SpringMVC与Struts2都可以替代原始Servlet技术。
SpringWebMVC框架提供了MVC(模型-视图-控制图)架构和用于开发灵活和松散耦合的Web应用程序的组件。MVC模式导致应用程序的不同方面(输入逻辑、业务逻辑和UI逻辑)分离,同时提供这些元素之间的松散耦合。
1、SpringMVC是几句代表性的MVC框架对初学者学习很有帮助;
a)模型(Model)封装了应用程序数据,通常它们将有Bean对象组成;
b)视图(View)负责渲染模型数据,一般来说它生成客户端浏览器可以解释HTML输出;
c)控制器(Controller)负责处理用户请求并构建适当的模型,并将其传递给视图进行渲染;
2、与Spring框架无缝集成;
3、对Servlet轻度封装,框架小巧轻便性能好;
4、可使用注解式开发,编码简洁;
5、便于测试;
6、具有本地化、国际化、类型转换、数据验证等功能;
二·、SpringMVC的简要处理流程
三、SpringMVC入门程序
1、创建web动态项目,导包。
2、配置好前端控制器,web.xml;其中,<servlet-mapping>配置了拦截规则,详见注释。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssm_springmvc</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 拦截规则
1、*.htm *.do *.action 以扩展名方式进行拦截 但不拦截静态资源 .js .jpsg .css 什么情况都可以使用
2、/ 不拦截jsp 但是拦截静态资源 .js .jpg .css RESTful风格 静态资源放行
3、/* 全都拦截 包括所有jsp资源以及静态资源,不推荐使用
-->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<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>
</web-app>
3、创建springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.dunka"/>
</beans>
4、导入前端资源,在webContent中引入静态资源,在/WEB-INF/中创立文件包jsp,放置item_list.jsp
5、根据页面的对象写商品实体对象
/**
*
* @author Dunka
* @Time 2019年2月17日
* @Todo 游戏实体信息 id name type price
*/
public class ItemInfo {
private String item_id;
private String item_name;
private String item_type;
private String item_price;
}
5、Controller 任务处理器
/**
*
* @author Dunka
* @Time 2019年2月17日
* @Todo 商品管理->游戏商品
*/
@Controller
public class ItemController {
//显示商品管理页面
@RequestMapping(value="/item/list.do")
public ModelAndView andView() {
ModelAndView modelAndView = new ModelAndView();
//向前端页面传递数据
ItemInfo info1 = new ItemInfo("1", "王者荣耀", "mmora", "0");
ItemInfo info2 = new ItemInfo("2", "吃鸡", "射击", "123");
ItemInfo info3 = new ItemInfo("3", "萌宠连连看", "休闲", "98");
ItemInfo info4 = new ItemInfo("4", "植物大战僵尸", "休闲", "0");
ItemInfo info5 = new ItemInfo("5", "愤怒的小鸟", "射击", "11");
List<ItemInfo> itemList = new ArrayList<ItemInfo>();
itemList.add(info1);
itemList.add(info2);
itemList.add(info3);
itemList.add(info4);
itemList.add(info5);
//先前台传递数据
modelAndView.addObject("itemList", itemList);
modelAndView.setViewName("/WEB-INF/jsp/item_list.jsp");
return modelAndView;
}
}
6、tomcat跑起来,OK。