您的位置:首页 > 其它

用户管理系统——添加用户

2016-03-01 10:59 337 查看


cstm.dao

public class CustomerDao {
private QueryRunner qr = new TxQueryRunner();

/**
* 添加客户
*
* @param c
*/
public void add(Customer c) {
try {
String sql = "insert into t_customer values(?,?,?,?,?,?,?)";
Object[] params = { c.getCid(), c.getCname(), c.getGender(),
c.getBirthday(), c.getCellphone(), c.getEmail(),
c.getDescription()};
qr.update(sql, params);
} catch(SQLException e) {
throw new RuntimeException(e);
}
}
}

cstm.service
public class CustomerService {
private CustomerDao customerDao = new CustomerDao();

/**
* 添加客户
* @param c
*/
public void add(Customer c) {
customerDao.add(c);
}

}

cstm.servlet
public class CustomerServlet extends BaseServlet {
private CustomerService customerService = new CustomerService();

/**
* 添加客户
*/
public String add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 1. 封装表单数据到Customer对象
* 2. 补全:cid,使用uuid
* 3. 使用service方法完成添加工作
* 4. 向request域中保存成功信息
* 5. 转发到msg.jsp
*/
Customer c = CommonUtils.toBean(request.getParameterMap(), Customer.class);
c.setCid(CommonUtils.uuid());
customerService.add(c);
request.setAttribute("msg", "恭喜,添加客户成功!");
return "f:/msg.jsp";
}
}
public abstract class BaseServlet extends HttpServlet{

@Override
public void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
req.setCharacterEncoding("UTF-8");
/*
* 1. 获取参数,用来识别用户想请求的方法
* 2. 然后判断是否哪一个方法,是哪一个我们就调用哪一个
*/
String methodName = req.getParameter("method");

if(methodName == null || methodName.trim().isEmpty()) {
throw new RuntimeException("您没有传递method参数!无法确定您想要调用的方法!");
}

/*
* 得到方法名称,是否可通过反射来调用方法?
* 1. 得到方法名,通过方法名再得到Method类的对象!
*   * 需要得到Class,然后调用它的方法进行查询!得到Method
*   * 我们要查询的是当前类的方法,所以我们需要得到当前类的Class
*/
Class c = this.getClass();//得到当前类的class对象
Method method = null;
try {
method = c.getMethod(methodName,
HttpServletRequest.class, HttpServletResponse.class);
} catch (Exception e) {
throw new RuntimeException("您要调用的方法:" + methodName + "(HttpServletRequest,HttpServletResponse),它不存在!");
}

/*
* 调用method表示的方法
*/
try {
String result = (String)method.invoke(this, req, resp);
/*
* 获取请求处理方法执行后返回的字符串,它表示转发或重定向的路径!
* 帮它完成转发或重定向!
*/
/*
* 如果用户返回的是字符串为null,或为"",那么我们什么也不做!
*/
if(result != null && !result.trim().isEmpty()) {

/*
* 查看返回的字符串中是否包含冒号,如果没有,表示转发
* 如果有,使用冒号分割字符串,得到前缀和后缀!
* 其中前缀如果是f,表示转发,如果是r表示重定向,后缀就是要转发或重定向的路径了!
*/

// 使用冒号分割字符串,得到前缀和后缀
int index = result.indexOf(":");//获取冒号的位置
if(index==-1) {
req.getRequestDispatcher(result).forward(req, resp);
}
else{
String s = result.substring(0, index);//截取出前缀,表示操作
String path = result.substring(index+1);//截取出后缀,表示路径
if(s.equalsIgnoreCase("r")) {//如果前缀是r,那么重定向!
resp.sendRedirect(req.getContextPath() + path);
} else if(s.equalsIgnoreCase("f")) {
req.getRequestDispatcher(path).forward(req, resp);
} else {
throw new RuntimeException("你指定的操作:" + s + ",当前版本还不支持!");
}
}

}
}catch (Exception e) {
System.out.println("您调用的方法:" + methodName + ", 它内部抛出了异常!");
throw new RuntimeException(e);
}
}
}

public class CommonUtils {
/**
* 生成不重复的32位长的大写字母串
* @return
*/
public static String uuid() {
return UUID.randomUUID().toString().replace("-", "").toUpperCase();
}

/**
* 把Map转换成指定类型的javaBean对象
* @param map
* @param clazz
* @return
*/
public static <T> T toBean(Map map, Class<T> clazz) {
try {
/*
* 1.创建指定类型的javaBean对象
*/
T bean = clazz.newInstance();
/*
* 2. 把数据封装到javabean中
*/
BeanUtils.populate(bean, map);
/*
* 返回javabean对象
*/
return bean;
} catch(Exception e) {
throw new RuntimeException(e);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: