SpringMVC配置

maven

核心依赖包


    
        org.springframework
        spring-core
        5.1.9.RELEASE
    
    
        org.springframework
        spring-beans
        5.1.9.RELEASE
    
    
        org.springframework
        spring-context
        5.1.9.RELEASE
    

Web依赖包


    org.springframework
    spring-webmvc
    5.1.9.RELEASE

配置文件

web.xml



    
        encoding
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        encoding
        /*
    
    
        log4jConfiguration
        classpath:log4j.properties
    
  Archetype Created Web Application
    
        contextConfigLocation
        /WEB-INF/applicationContext.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
        dispatcher
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            /WEB-INF/dispatcher-servlet.xml
        
        1
    
    
        dispatcher
        /
    

dispatcher-servlet.xml




    
    
        
    

    
    

    
    

    
    
    

    

    
    
        
        
    
    
    
    
        
    
    
        
        
        
        
        
        
        
        
        
    

applicationContext.xml

配置除Controller以外需进行依赖注入的类



    
        
    

    


    
        
        
        
        
    

    
        
        
        
            
            
                
            
        
    

    
        
        
    


    

    

Controller

请求

@Controller
@RequestMapping(value = "/") //拦截根目录
public class IndexController {

    @RequestMapping(value = "")
    public String index(){
        return "index"; //返回到index页面
    }
}
  • 带参
    • 普通方式
    // url=/view?name=123
    @RequestMapping(value = "/view",method = RequestMethod.GET)
    public String index(@RequestParam(value = "name") String name){
        return "index";
    }
    
    • Restful方式
    // url=/view/2
    @RequestMapping(value = "/view/{name}",method = RequestMethod.GET)
    public String index(@PathVariable(value = "name") String name){
        return "index";
    }
    
    • servlet方式
    // url=/view?name=123
    @RequestMapping(value = "",method = RequestMethod.GET)
    public String index(HttpServletRequest request){
        int id = Integer.valueOf(request.getParameter("name"));
        return "index";
    }
    

重定向

@Controller
@RequestMapping(value = "/")
public class IndexController {

    @RequestMapping(value = "",method = RequestMethod.GET)
    public String index(){
        return "index";
    }

    @PostMapping(value = "subDoSth")
    public String doSth(){
    //重定向到指定位置,跳转使用forward
        return "redirect:/";
    }

}

参数访问

  • PathVariable方式

/url/demo/param/p/123

@GetMapping(value = "demo/param/p/{message}")
public String demoMsg(@PathVariable String message){
    return "hello "+message;
}
  • RequestParam

/url/demo/param?p=123

@GetMapping(value = "demo/param")
public String demoMsg(@RequestParam(value = "p") String message){
    return "hello "+message;
}

你可能感兴趣的:(SpringMVC配置)