日期:2014-05-16 浏览次数:20578 次
package com.cfuture08.eweb4j.demo.petstore.pet.web; import java.util.Arrays; import com.cfuture08.eweb4j.demo.petstore.AbstractAction; import com.cfuture08.eweb4j.demo.petstore.pet.Pet; import com.cfuture08.eweb4j.demo.petstore.pet.PetException; import com.cfuture08.eweb4j.mvc.annotation.POST; import com.cfuture08.eweb4j.mvc.annotation.Path; /** * 添加宠物信息控制器 * * @author weiwei * */ public class AddPetInfoAction extends AbstractAction { /** * 接收页面表单数据,提供验证规则 */ private Pet pet = null; /** * 处理HTTP请求 POST http://{host}:{port}/eweb/pets/ * * @return */ @Path("pets/") @POST public String doPost() { request.setAttribute("types", Arrays.asList("猫", "狗", "猪")); try { petService.create(pet); } catch (PetException e) { request.setAttribute("error", e.getMessage()); return "../error.jsp"; } return "redirect:list"; } public Pet getPet() { return pet; } public void setPet(Pet pet) { this.pet = pet; } }
package com.cfuture08.eweb4j.demo.petstore; import javax.servlet.http.HttpServletRequest; import com.cfuture08.eweb4j.demo.petstore.master.MasterService; import com.cfuture08.eweb4j.demo.petstore.pet.PetService; import com.cfuture08.eweb4j.ioc.annotation.Ioc; /** * 宠物信息抽象控制器 * * @author weiwei * */ public abstract class AbstractAction { /** * 通过IOC容器注入 */ @Ioc("petService") protected PetService petService; /** * 通过IOC容器注入 */ @Ioc("masterService") protected MasterService masterService; /** * 声明且提供setter方法,框架就能自动注入 */ protected HttpServletRequest request; public PetService getPetService() { return petService; } public void setPetService(PetService petService) { this.petService = petService; } public MasterService getMasterService() { return masterService; } public void setMasterService(MasterService masterService) { this.masterService = masterService; } public HttpServletRequest getRequest() { return request; } public void setRequest(HttpServletRequest request) { this.request = request; } }
package com.cfuture08.eweb4j.demo.petstore.pet; import java.util.List; import com.cfuture08.eweb4j.orm.dao.DAO; import com.cfuture08.eweb4j.orm.dao.DAOException; import com.cfuture08.eweb4j.orm.dao.cascade.CascadeDAO; import com.cfuture08.eweb4j.orm.dao.delete.DeleteDAO; import com.cfuture08.eweb4j.orm.dao.factory.DAOFactory; import com.cfuture08.eweb4j.orm.dao.insert.InsertDAO; import com.cfuture08.eweb4j.orm.dao.select.DivPageDAO; import com.cfuture08.eweb4j.orm.dao.select.SelectDAO; import com.cfuture08.eweb4j.orm.dao.update.UpdateDAO; /** * 宠物信息服务接口实现类 * * @author weiwei * */ public class PetServiceImpl implements PetService { private DivPageDAO divPageDAO; private CascadeDAO cascadeDAO; private SelectDAO selectDAO; private InsertDAO insterDAO; private UpdateDAO updateDAO; private DeleteDAO deleteDAO; // 这是一个用来拼凑sql语句的接口, // 是在ORM映射之下的.并不是直接拼凑原生SQL private DAO dao; public PetServiceImpl() { this.divPageDAO = DAOFactory.getDivPageDAO(); this.cascadeDAO = DAOFactory.getCascadeDAO(); this.selectDAO = DAOFactory.getSelectDAO(); this.insterDAO = DAOFactory.getInsertDAO(); this.updateDAO = DAOFactory.getUpdateDAO(); this.deleteDAO = DAOFactory.getDeleteDAO(); this.dao = DAOFactory.getDAO(Pet.cl