Maven + SSM 框架

web.xml



    Archetype Created Web Application

    
    
    
        dispatcher
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring/spring-*.xml
        
        
        2
    

    
    
        dispatcher
        /
    

    
        encodingFilter
        
            org.springframework.web.filter.CharacterEncodingFilter
        
        
            encoding
            utf-8
        
    

    
        encodingFilter
        /*
    


mybatis-config.xml





    
        
        
        
        
        
        
        
        
        
        
    

spring文件夹下三个xml配置文件,spring-dao.xml,spring-service.xml,spring-mvc.xml
spring-dao.xml




    
    
    

    
    
        
        
        
        
    

    
    
        
        
        
        

        
        
        
        
        
    

    
    
        
        
        
        
        
        
    


spring-service.xml




    
    

    
    
        
    

    
    


spring-mvc.xml




    
    
    

    
    
    

    
    

    
    
    
        
        
        
    


Controller
HomeController

package com.shpun.controller;

@Controller
@RequestMapping("/home")
public class HomeController {

    @Autowired
    private CustomerService customerService = null;

    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public ModelAndView getHome(){
        List customerList = customerService.selectAllCustomer();

        ModelAndView mv = new ModelAndView();
        mv.setViewName("Home");
        mv.addObject("customerList",customerList);

        return mv;
    }

}

CustomerController

package com.shpun.controller;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService = null;

    // 跳转到添加Customer的jsp
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public ModelAndView addCustomer(){
        System.out.println("to AddCustomer.jsp");

        ModelAndView mv = new ModelAndView();
        mv.setViewName("AddCustomer");
        return mv;
    }

    // 添加完Customer后重定向到home的jsp
    @RequestMapping(value = "/toAdd",method = RequestMethod.POST)
    public String toAddCustomer(Customer customer){
        System.out.println("to insert customer:"+customer);
        customerService.insertCustomer(customer);
        System.out.println("to insert over customer:"+customer);

        return "redirect:/home/get";
    }

    // 删除完Customer后重定向到home的jsp
    @RequestMapping(value = "/toDelete",method = RequestMethod.GET)
    public String toDeleteCustomer(@RequestParam("id")String id){
        System.out.println("to delete customer id :"+id);
        customerService.deleteCustomer(id);
        System.out.println("to delete over customer id :"+id);

        return "redirect:/home/get";
    }

    // 跳转到更新Customer的jsp
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView editCustomer(@RequestParam("id")String id){
        System.out.println("to EditCustomer.jsp :");

        Customer customer = customerService.selectCustomer(id);

        ModelAndView mv = new ModelAndView();
        mv.setViewName("EditCustomer");
        mv.addObject("customer",customer);
        return mv;
    }

    // 更新完Customer后重定向到home的jsp
    @RequestMapping(value = "/toEdit",method = RequestMethod.POST)
    public String toUpdateCustomer(Customer customer){
        System.out.println("to update customer :"+customer);
        customerService.updateCustomer(customer);
        System.out.println("to update over customer :"+customer);

        return "redirect:/home/get";
    }

    // 跳转到查询Customer页面
    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public ModelAndView searchCustomer(){
        System.out.println("to SearchCustomer.jsp :");

        ModelAndView mv = new ModelAndView();
        mv.setViewName("SearchCustomer");
        return mv;
    }

    // 跳转到查询Customer页面
    @RequestMapping(value = "/toSearch",method = RequestMethod.POST)
    public ModelAndView toSearchCustomer(Customer customer){
        System.out.println("to search");

        List customerList = customerService.searchCustomers(customer);

        ModelAndView mv = new ModelAndView();
        mv.setViewName("SearchCustomerResult");
        mv.addObject("customerList",customerList);
        return mv;
    }

}

PS:

springMVC传值到jsp失败,可能是忘记修改web.xml的webapp


参考:超详细IDEA+Maven 整合SSM框架实现简单的增删改查

你可能感兴趣的:(Maven + SSM 框架)