1.定义一个IMessageService接口
package cn.zwb.service;
import java.util.Map;
import java.util.Set;
import cn.zwb.vo.Message;
public interface IMessageService {
public boolean insert(Message vo) throws Exception;
public boolean delete(Set<Integer> ids)throws Exception;
public boolean update(Message vo) throws Exception;
public Message get(int id)throws Exception;
public Map<String,Object> list(String column,String keyWord,int currentPage,int lineSize)throws Exception;
}
本程序的业务层里面充分的考虑到了几乎所有可能出现的情况,而且也要涉及到参数的传递问题,
2.定义这个接口实现类,所有的操作方法都是假实现;
package cn.zwb.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import cn.zwb.service.IMessageService;
import cn.zwb.vo.Message;
import cn.zwb.vo.Type;
@Service
public class MessageServiceImpl implements IMessageService {
@Override
public boolean insert(Message vo) throws Exception {
System.out.println("********[增加新消息]*******");
return true;
}
@Override
public boolean delete(Set<Integer> ids) throws Exception {
System.out.println("======[删除消息数据]===========");
return true;
}
@Override
public boolean update(Message vo) throws Exception {
System.out.println("======[更新消息数据]===========");
return true;
}
@Override
public Message get(int id) throws Exception {
System.out.println("******[根据ID查询数据]*************");
Message msg=new Message();
msg.setMid(10);
msg.setPrice(999.9);
msg.setPubdate(new Date());
msg.setTitle("ZHAN WEN BO");
Type type = new Type();
type.setType("教育新闻");
msg.setType(type);
return msg;
}
@Override
public Map<String, Object> list(String column, String keyWord, int currentPage, int lineSize) throws Exception {
Map<String, Object>map=new HashMap<String, Object>();
List<Message> all=new ArrayList<Message>();
for(int x=(currentPage-1)*lineSize;x<currentPage*lineSize;x++){
Message msg=new Message();
msg.setMid(10+x);
msg.setPrice(999.9+x);
msg.setPubdate(new Date());
msg.setTitle("ZHAN WEN BO-"+x);
Type type = new Type();
type.setType("教育新闻-"+x);
msg.setType(type);
}
map.put("allMessage",all);
map.put("messageCount",888);
return map;
}
}
3.既然整个代码都在Spring的控制之中,那么就可以使用依赖注入的方式在Action注入服务层接口
随后为了更好的模拟,编写一个增加数据的表单.
4.定义message_insert.jsp页面;
<%@ page language="java" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
public void initBinder(WebDataBinder binder){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//注册一个专门的日期转换的操作类,并且允许输入为空
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf,true ));
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"></head> <body><form action="pages/back/message/message_insert.action" method="post">消息编号:<input type="text" id="mid" nmae="mid" value="999"><br>消息名称:<input type="text" id="title" nmae="title" value="啦啦啦"><br>消息价格:<input type="text" id="price" nmae="price" value="99999.99"><br>发布日期:<input type="text" id="pubdate" nmae="pubdate" value="2001-09-09"><br>消息类型:<input type="text" id="type.title" nmae="type.title" value="今日头条"><br><input type="submit" value="提交"><input type="reset" value="重置"></form></body></html>
范例:定义message_update.jsp页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
</head>
<body>
<form action="pages/back/message/message_update.action" method="post">
消息编号:<input type="text" id="mid" name="mid" value="999"><br>
消息名称:<input type="text" id="title" name="title" value="啦啦啦"><br>
消息价格:<input type="text" id="price" name="price" value="99999.99"><br>
发布日期:<input type="text" id="pubdate" name="pubdate" value="2001-09-09"><br>
消息类型:<input type="text" id="typeTitle" name="typeTitle" value="今日头条"><br>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>
接收的方法上再加一个参数
@RequestMapping("message_update")
public ModelAndView update(Message msg,Type type) throws Exception{
msg.setType(type);
System.out.println(msg);
System.out.println(this.messageService.update(msg));
ModelAndView mav=new ModelAndView("/forward.jsp");
mav.addObject("msg", "消息更新更新成功").addObject("url","index.jsp");
return mav;
}
但是参数不能重名
以上的程序如果直接执行,只要没有设计到日期问题,那么都可以正常提交但是如果有了日期操作,那么这个就必须明确的定义一个转换器(一般都会在父类的控制层定义)
public void initBinder(WebDataBinder binder){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//注册一个专门的日期转换的操作类,并且允许输入为空
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf,true ));
}
这个方法一般都定义在父类上
6.为了解决项目中了乱码问题,一般有两种方式:
方式一:自己建立一个过滤器来解决提交乱码的问题;
方式二:使用Spring中提供的一个完整过滤器
org.springframework.web.filter.CharacterEncodingFilter
但是在配置的时候要设置好相应的编码
范例:在web.xml文件里面配置过滤器;
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
7.那么如果说现在的属性就是重名,那么自动将所有的内容都给了属性内容
8.在进行非vo类型数据传输的时候,往往会直接传递一个具体的参数名称.
例如,现在要进行数据的删除操作,从道理上来讲应该传递如下的内容;
http://localhost:8080/SpringMVCProject/pages/back/message/message_delete.action?ids=1,2,3
当然这个ids可以是其他名字,但是要在接收处理的方法上加上一下注解,比如属性名称为paramids,就得添加一下注解
public ModelAndView delete(@RequestParam(value="paramids",defaultValue="0")String ids){}
使用"@RequestParam"有一个最大的好处是它可以设置没有传递参数时的参数默认值.这一点对于分页是有好处的,但是与重复的编写注解相比,个人更愿意来处理分页参数
现在等于Spring帮助用户解决了参数是否重复的判断,和参数类型的自动转换操作问题
http://localhost:8080/SpringMVCProject/pages/back/message/message_get.action?mid=1
在方法参数上定义的是int,Spring自动完成转换操作
又比如分页操作
9.分页处理
如果要调用分页,则地址信息如下:
http://localhost:8080/SpringMVCProject/pages/back/message/message_list.action?column=啦啦&keyWord=还是啦啦¤tPage=1&lineSize=5
但是很多时候我们可能不会去编写这样的参数