1 jsp/customer/add.jsp
完整代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>添加客户
2 CustomerAction
完整代码:
package www.test.web.action;import java.io.File;import org.apache.commons.lang3.StringUtils;import org.hibernate.criterion.DetachedCriteria;import org.hibernate.criterion.Restrictions;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import www.test.domain.Customer;import www.test.service.CustomerService;import www.test.utils.PageBean;public class CustomerAction extends ActionSupport implements ModelDriven{ private Customer customer = new Customer(); private CustomerService cs; private Integer currentPage; private Integer pageSize; //上传的文件会自动封装到File对象中 //在后台提供一个与前台 组件name相同的属性即可。 private File photo; //在提交的键名后加上固定的后缀FileName,文件的名称会自动封装到属性中。 private String photoFileName; //在提交的键名后加上固定的后缀ContentType,文件的MIME类型值会自动封装到属性中。 private String photoContentType; //获取客户列表 public String list() throws Exception { //封装离线查询对象 DetachedCriteria dc = DetachedCriteria.forClass(Customer.class); //判断并封装参数 if(StringUtils.isNotBlank(customer.getCust_name())){ dc.add(Restrictions.like("cust_name", "%"+customer.getCust_name()+"%")); } //1 调用Service查询分页数据(PageBean) PageBean pb = cs.getPageBean(dc,currentPage,pageSize); //2 将PageBean放入request域,转发到列表页面显示 ActionContext.getContext().put("pageBean", pb); return "list"; } //保存客户 public String add() throws Exception { System.out.println("文件名称:"+photoFileName); System.out.println("文件MIME类型:"+photoContentType); //将上传文件保存到指定位置 //renameTo相当于剪切==>复制 photo.renameTo(new File("C:/Users/jepson/Pictures/Saved Pictures/"+photoFileName)); //=============================================== //调用service,保存Customer对象 cs.save(customer); //重定向到Action return "toList"; } @Override public Customer getModel() { return customer; } public CustomerService getCs() { return cs; } public void setCs(CustomerService cs) { this.cs = cs; } public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public File getPhoto() { return photo; } public void setPhoto(File photo) { this.photo = photo; } public String getPhotoFileName() { return photoFileName; } public void setPhotoFileName(String photoFileName) { this.photoFileName = photoFileName; } public String getPhotoContentType() { return photoContentType; } public void setPhotoContentType(String photoContentType) { this.photoContentType = photoContentType; }}