°¥£¬½ñÌìcode viewµÄʱºò°¤ÅúÁË£¬ÔÒòºÜ¼òµ¥£¬¾ÍÊDZ¾È˵Ä
±àÂë·ç¸ñ²»·ûºÏ
ËûÃǹ«Ë¾µÄ½á¹¹·Ö²ã£¨±¾È˸ÕÀ´Õâ¸ö¹«Ë¾Ò»¸öÔ£©£¬ÏÂÃæ¾ßÌå˵һϾßÌåÔÒò¡£
¹«Ë¾µÄ±àÂë·ç¸ñÊÇ·ÖΪËIJãµÄpo¡¢dao¡¢service¡¢control¡£poÊÇÊý¾Ý¿âÖбí¶ÔÓ¦µÄʵÌåÀ࣬Õâ¸öºÁÎÞÒÉÒ壬daoÊǶÔpoµÄÊý¾Ý¿â²Ù×÷
½Ó¿Ú(¼´Êý¾Ý¿âµÄÔöɾ¸Ä²é)Õâ¸öҲû ÓÐÒìÒ飬ÎÊÌâ³öÔÚÁËserviceºÍcontrol²ã¡£ÎÒ
·¢ÏÖ¹«Ë¾Ð´µÄservice²ã¶¼ÊÇαservice£¬ÆäÖÐдµÄ½Ó¿ÚÊdzàÂãÂãµÄ¶Ôdao²ãµÄÕհᣬ¶øcontrol²ãÀïÃ澹Ȼ³öÏÖÁ˺ܶàÒµÎñÂß¼´úÂ룬ÈçÏ£¨ÎÒ¾ÍÖ±½ÓÌù³öservice½Ó¿ÚµÄʵÏÖ´úÂëÁË£©
@
Service("areaService")
public
class AreaServiceImpl implements IAreaService{
@Autowired
@Qualifier("areaDao")
private IAreaDao areaDao;
public List<Area> getByTeaClass(Long teaId,Long classId){
return areaDao.findByTeaClass(teaId,Long classId);
}
public void save(Area area){
areaDao.save(area);
}
}
@Controller("areaAction")
public class AreaAction extends BaseAction {
@Autowired
@Qualifier("areaService")
private IAreaService areaService;
public ActionForward doAdd(ActionMapping mapping, ActionForm form,
Http
ServletRequest request, HttpServletResponse response) {
String areaName=request.getParameter("areaName");
Area area = new Area();
area.setAreaName(areaName);
areaService.save(area);
return mapping.findForward("success");
}
}
µ±È»ÎÒÖ»ÊÇΪÁËÑÝʾһÏ´úÂ룬ÒÔÉÏдµÄ±È½Ï¼òµ¥£¬ÆäÖеÄ
Òì³£´¦ÀíÒÔ¼°ÈÕÖ¾¶¼Ã»Ð´£¬ÒÔÉϾÍÊǹ«Ë¾µÄ´úÂëд·¨¡£ÎÒÈÏΪÒÔÉÏ´úÂëµÄservice²ãÍêȫûÓг䵱ÕæÕýservice²ãµÄ
ÔðÈΣ¬Ö»ÊÇÕÕ°áÁËdao²ãµÄ·½·¨£¬È»ºóÔÙactionÖе÷Ó㬷´¶øÔÚcontrolÖнøÐÐÁ˸÷ÖÖÂß¼µÄ´¦Àí£¨µ±È»ÔÚmvcÈý²ã
¼Ü¹¹ÀÕâÑùдÊǺÁÎÞÒÉÒåµÄ£¬µ«ÊÇÏÖÔÚ¼ÓÈëÁËservice²ã£©¡£
¶øÎÒµÄservice²ãÊÇ´¦ÀíÒµÎñÂß¼µÄ£¬²¢ÇÒ°üÀ¨¸÷ÖÖÂß¼µÄ´¦Àí£¬²¢Çҳе£actionÖпØÖÆÁ÷³ÌµÄ²¿·Ö£¬ÈÃactionÖ»¹ØÐÄҪתµ½µÄλÖã¬ÏÂÃæÌù³öÎҵĴúÂë
@Service("areaService")
public class AreaServiceImpl implements IAreaService{
@Autowired
@Qualifier("areaDao")
private IAreaDao areaDao;
public HttpServletRequest save(HttpServletRequest request){
String areaName=request.getParameter("areaName");
Area area = new Area();
area.setAreaName(areaName);
areaDao.save(area);
}
}
@Controller("areaAction")
public class AreaAction extends BaseAction {
@Autowired
@Qualifier("areaService")
private IAreaService areaService;
public ActionForward doAdd(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
areaService.save(request);
return mapping.findForward("success");
}
}
ÒÔÉÏÊÇÎÒµÄд·¨£¬ÏÂÃæ˵һ˵ÎÒÕâÑùдµÄÏë·¨:
Ê×ÏÈÀ´ËµÔÚMVC¿ò¼ÜÖÐ
controller³Ðµ£ÁËÁ½ÖÖÔðÈΣ¬Ò»¸öÊÇÂß¼Á÷³ÌµÄ¿ØÖÆ£¬»¹ÓÐÒ»¸öÊÇÒ³ÃæµÄÌøת£¬Èç¹û°´ÕÕÉÏÃ湫˾µÄд·¨µÄ»°£¬ÊƱØcontrollerÖеĴúÂëÁ¿»áºÜ´ó£¬ÔÙ¼ÓÉÏV²ã£¬Õâ¾Íµ¼ÖÂÁËMVC¿ò¼ÜµÄÁ½Í·´ó£¬ÖмäϸµÄ½á¹û£¬¶øÉÏÃ湫˾µÄд·¨¼ÓÉÏserviceÒ»µãÓô¦¶¼Ã»ÓУ¬ÍêÈ«¿ÉÒÔ²»ÓÃдservice²ã¡£¶øµÚ¶þÖÖд·¨ÄØ£¬service½«ËùÓеÄÒµÎñÂß¼µÄ»îÈ«¶¼À¿ÁËÏÂÀ´£¬°üÀ¨´ÓÇëÇóÖлñÈ¡Êý¾Ý£¬È»ºó½«Êý¾Ý·â×°³ÉʵÌåÀ࣬Ȼºó½«ÊµÌåÀà³Ö¾Ã»¯£¬ËùÓеÄÒµÎñÂß¼¶¼ÔÚservice²ãÀïÃæȥʵÏÖ£¬ÈÃcontrollerÖ»¹ØÐÄÌøת£¬ÕâÒ²±ÜÃâÁËÁ½Í·´óÖмäϸµÄ¾ÖÃæ¡£ÆäʵµÚ¶þÖÖд·¨ÊÇΪcontroller·Öѹ£¬½«ÔÀ´controllerÖÐÒµÎñÁ÷ת²¿·Ö¶¼Äõ½service²ãȥʵÏÖ¡£
ÎÒÈÏΪÕâÑù¼Óservice²ã²ÅÓбØÒª£¬·ñÔò¸ù±¾Ã»±ØÒª¼Óservice²ã£¬Ö»ÐèmvcÈý²ã¾Í¿ÉÒÔÁË¡£
ÎÒ
Àí½âµÄserviceÓÐÁ½ÖÖ¹¦ÄÜ£¬Ò»¸öÊÇÍêÈ«´¦ÀíÒµÎñÂß¼£¬»¹ÓÐÒ»¸öÊÇÏòÍâÌṩ½Ó¿Ú£¬Èç¹û°´ÕÕµÚÒ»ÖÖд·¨µÄ»°£¬ÎҸоõÕæû±ØÒª¼Óservice²ã¡£