1.
第一步:整合DAO层。
通过spring管理mapper接口
使用mapper的扫描器自动扫描mapper接口在spring中进行注册。
第二部:整合service层
通过spring管理service接口。
使用配置方式将service接口配置在spring文件中
第三步:整合springMVC
由于springMVC是spring的模块,不需要整合。
2.
在class上添加@requestMapping(url)指定通用请求。
@requestMapping(“/url”)
还可以限制http请求放法(value="/url",method={RequestMethod.post/get})
3. controller返回值
1.返回modelview, 需要在放法结束时,定义modelview。
@RequestMapping("/url")
public ModelAndView test()throws Exception{
//调用service更新商品信息
// 返回ModelAndView
ModelAndView andView = new ModelAndView();
// 保存商品到MODEL
andView.addObject("test", test);
// 在视图里已经配置好前缀和后缀
andView.setViewName("items/editItems");
return andView;
}2.返回string ,表示返回逻辑视图名
public String test(Model model)throws Exception{
/*// 返回ModelAndView
ModelAndView andView = new ModelAndView();
// 保存商品到MODEL
andView.addObject("itemsCustom", itemsCustom);
// 在视图里已经配置好前缀和后缀
andView.setViewName("items/editItems");*/
model.addAttribute("test", test);
return "test/test";
}
3.redirect重定向,浏览器地址栏中的url会变化。
@RequestMapping("/url")
public String test()throws Exception{
return "redirect:url.action";
}
4.forward转发:浏览器地址栏中的url不会变化
@RequestMapping("/url")
public String test()throws Exception{
return "forward:url.action";
}5.返回void:
在controller放法形参上可以定义request和response,使用这两个可以响应结果。
1.使用request转发页面: request。getrequestDispatcher("url").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.参数绑定?
POst乱码问题;
在web.xml添加post乱码过滤器(filter);
<!-- post乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
get乱码:
1. 修改tomcat配置文件添加乱码与工程编码一致。
<connector URLEncoding="utf-8" connectionTimeout="2000" port="8080" ...>
2.对参数进行重新编码:
String name=new String(request.getParamter("username").getBytes("iso8859-1"),"utf-8");
5.自定义参数绑定;
日期类。需要转换成pojo类的一致。
springmvc.xml
<!-- 注解映射器 -->
<!-- 注解适配器 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven><!-- 自定义参数绑定 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 转换器 -->
<property name="converters">
<list>
<!-- 日期类型转换 -->
<bean class="cn.lanz.sm.controller.converter.CustomDateConverter"/>
</list>
</property>
</bean>public class CustomDateConverter implements Converter<String, Date>{
@Override
public Date convert(String source) {
// TODO Auto-generated method stub
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
try {
return dateFormat.parse(source);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}6.springMVC和struts区别?
1.springMVC是基于方法--类似于service开发。struts2基于类的开发。
2.springMVC可以进行单列开发,并且建议使用单列开发。struts2通过类的成员变量来接收参数,无法使用单列,只能使用多列
3.struts2速度慢,在于使用struts2标签。建议使用jslt标签

本文介绍 Spring MVC 的整合步骤,包括 DAO 层、Service 层和服务端控制器的配置方法。同时,详细解释了控制器不同类型的返回值及其实现方式,如 ModelView、字符串、重定向等。

2万+

被折叠的 条评论
为什么被折叠?



