什么是SpringMVC?
1、springmvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合。
2、springmvc是一个基于mvc的web框架。
2、springmvc是一个基于mvc的web框架。
springMVC框架
通过策略接口,Spring框架是高度可配置的,而且包含多种视图技术,JSP,Velocity, Tiles,Itext 和POI. SpringMVC框架并不知道使用的视图,不会强迫开发者只使用JSP技术。SpringMVC分离了控制器,模型对象,过滤器以及处理程序对象的角色,这种分离让他们更容易进行定制。
其中的组件有:前端控制器、处理器映射器、处理器适配器、处理器Handler(需要程序员开发)、视图解析器、视图View(需要程序员开发)
相关配置
在Web.xml中配置前端控制器
在Springmvc.xml中配置处理器适配器、处理器映射器、Handler、视图解析器
不过,常用的还是配置注解映射器和适配器
SpringMVC中的配置变成了如下:
<!-- 可以扫描controller、service、...
这里让扫描controller,指定controller的包
-->
<context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- 视图解析器
解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包
-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 配置jsp路径的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
Springmvc+mybatis架构
配置:
在web.xml中添加Spring容器监听器,加载Spring容器
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
常用注解
@Controller--声明控制器
@RequestMapping--声明URL
1、url映射:定义controller方法对应的url,进行处理器映射使用
2、窄化请求映射:
//为了对url进行分类管理 ,可以在这里定义根路径,最终访问url是根路径+子路径
//比如:商品列表:/items/queryItems.action
@RequestMapping("/items")
3、限制http请求方法:
//限制http请求方法,可以post和get
// @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
@Autowired--业务接口注入
@Autowired
private ItemsService itemsService;
Controller方法的返回值+往视图传数据
1、返回ModelAndView(直接用ModelAndView的addobject来往视图传数据)
@RequestMapping("/queryItems")
public ModelAndView queryItems(HttpServletRequest request) throws Exception {
// 调用service查找 数据库,查询商品列表
List<ItemsCustom> itemsList = itemsService.findItemsList(null);
// 返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
// 相当 于request的setAttribut,在jsp页面中通过itemsList取数据
modelAndView.addObject("itemsList", itemsList);
// 指定视图
// 下边的路径,如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀,修改为
// modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
// 上边的路径配置可以不在程序中指定jsp路径的前缀和jsp路径的后缀
modelAndView.setViewName("items/itemsList");
return modelAndView;
}
2、返回string(
用model往页面传数据
)
①表示返回逻辑视图名
真正视图(jsp路径)=前缀+逻辑视图名+后缀
public String editItems(Model model,@RequestParam(value="id",required=true) Integer items_id)throws Exception {
//调用service根据商品id查询商品信息
ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
//通过形参中的model将model数据传到页面
//相当于modelAndView.addObject方法
model.addAttribute("itemsCustom", itemsCustom);
return "items/editItems";
}
其中@requestParam(value="id" :是指,前面页面上控件name属性是id,但是我们想在后台Controller里面写别的参数名(items_id),就可以用这种写法,也可以保持一致。
其中required属性表示,是否必须要传入。
②redirect重定向
public String editItemsSubmit(HttpServletRequest request,Integer id,ItemsCustom itemsCustom)throws Exception {
//调用service更新商品信息,页面需要将商品信息传到此方法
itemsService.updateItems(id, itemsCustom);
//重定向到商品查询列表
return "redirect:queryItems.action";
return "success";
}
特点:浏览器地址栏中的url会变化。修改提交的request数据无法传到重定向的地址。因为重定向后重新进行request(request无法共享)
③forward页面转发
public String editItemsSubmit(HttpServletRequest request,Integer id,ItemsCustom itemsCustom)throws Exception {
//调用service更新商品信息,页面需要将商品信息传到此方法
itemsService.updateItems(id, itemsCustom);
//页面转发
//return "forward:queryItems.action";
return "success";
}
特点:浏览器地址栏url不变,request可以共享。
3、返回void
在controller方法形参上可以定义request和response,使用request或response指定响应结果:
1、使用request转向页面,如下:
request.getRequestDispatcher("页面路径").forward(request, response);
2、也可以通过response页面重定向:
response.sendRedirect("url")
3、也可以通过response指定响应结果,例如响应json数据如下:
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");
1、使用request转向页面,如下:
request.getRequestDispatcher("页面路径").forward(request, response);
2、也可以通过response页面重定向:
response.sendRedirect("url")
3、也可以通过response指定响应结果,例如响应json数据如下:
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");
4、其他数据往jsp上传的方法
@ModeAttribute
最后一种:简单的数据传递:
model.addAttribute("id", id);
参数绑定+视图往Controller传数据
注意:springmvc中,接收页面提交的数据是通过方法形参来接收。而不是在controller类定义成员变更接收
1、 默认支持的类型:HttpServletRequest、HttpServletResponse、HttpSession、Model/ModelMap
2、简单类型:在形参中 添加HttpServletRequest request参数,通过request接收查询条件参数。
public String editItemsSubmit(HttpServletRequest request,Integer id,ItemsCustom itemsCustom)throws Exception
本来,直接用httpservletRequest request,就可以把前面控件有name的值的数据传到Controller了,但是这里为什么要再写一遍integer id,这个意思是代码良好的象征,告诉别人,我这个方法,要传入的这个id,就是你所需要用的。
3、pojo绑定
public String editItemsSubmit(HttpServletRequest request,Integer id,ItemsCustom itemsCustom)throws Exception
这里的简单类型:integer id,可以和pojo绑定一起使用。
另外:还可以传入包装类型的pojo接收视图中的参数
jsp中:
<input name="itemsCustom.name" />
Controller中:
public ModelAndView queryItems(HttpServletRequest request,ItemsQueryVo itemsQueryVo) throws Exception
包装类型pojo中:
public class ItemsQueryVo{
private ItemsCustom itemsCustom;
}
4、数组绑定
jsp中:
<c:forEach items="${itemsList }" var="item">
<tr>
<tr>
<td><input type="checkbox" name="items_id" value="${item.id}"></td>
<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 }/items/editItems.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
<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 }/items/editItems.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
Controller中:
public String delete(Integer[] items_id) throws Exception{}
5、List绑定
先在包装pojo 中定义list<pojo>属性,把他当成普通的属性去用。
public class ItemsQueryVo {
//商品信息
private Items items;
//为了系统 可扩展性,对原始生成的po进行扩展
private ItemsCustom itemsCustom;
private List<ItemsCustom> itemsList;
controller中:
public String editItems(ItemsQuery vo itemsQueryVo){}
jsp上:
<c:forEach items="${itemsList}" var="item" varStatus="status">
<tr>
<td> <input name="itemsList"[${status.index}].name value=""></td>
<td> <input name="itemsList"[${status.index}].name value=""></td>
</tr>
</c:forEach>
【小结】
Springmvc还有很多高级的用法,有机会的话,小编在一一为大家解读。