springMVC控制实例

1、新建一个web工程项目导入相关的架包

需要导入的包大致有:

springMVC控制实例_第1张图片
架包

其中除了spring的架包之外还包括链接mysql数据的包,EL表达式的架包,以及tomcat的一些servlet包

2、对spring的applicationContext.xml的文件进行配置:

其中包括该项目的 异常处理和拦截过滤器,其大致配置如下



    
        
        
        
        
    


    
    
    

    
        
        
    

    
    
        
            
                error
            
        
    

    
    
        
            
            
            
            
            
            
        
    

3、对servlet的web.xml文件进行配置

  
        action
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:applicationContext.xml
        
        
        1
    

    
        action
        *.do
    

    

    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        encodingFilter
        /*
    

4、在entry包下写下这次业务所需要的实体类
这里就不在列出

5、在service包下写出这次业务的服务层


@Service
//服务层多用@service注入到springBean中
public class CostService {
//    注入需要的DAO,可以与数据库交流
    private CostDao costDao;

    public CostDao getCostDao() {
        return costDao;
    }

    @Resource(name="costDao")
//    在set方法上用@Resource实行注入,不建议在属性上注入
    public void setCostDao(CostDao costDao) {
        this.costDao = costDao;
    }

    public List findAll(){
        return costDao.findAll();
    }
}

6、在controller包下写出这次业务的控制层

//需要在类上加上controller注解,注册bean,用equestMapping设置规划的路径
@Controller
@RequestMapping("/cost")
public class CostController {
//    注入服务业务
    private CostService costService;

    public CostService getCostService() {
        return costService;
    }

    @Resource(name="costService")
    public void setCostService(CostService costService) {
        this.costService = costService;
    }

//    规划路径
    @RequestMapping("/find.do")
    public String find(ModelMap model){
        List list=costService.findAll();
        model.addAttribute("costs",list);
        return "cost/cost_list";
    }
}

你可能感兴趣的:(springMVC控制实例)