spring boot 整合springmvc 各种请求?

@RequestMapping 配置 url 映射

@Controller 处理 http 请求

@RestController 处理 ajax 请求

@PathVariable 获取 url 参数

@RequestParam 获取请求参数

前台处理:

<button onclick="show()">点我点我</button>
<a href="/blog/21">点我博客</a>
<a href="/blog/query?q=123456">搜索</a>

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
	
	function show(){
		$.post("ajax/hello",{},function(result){
			alert(result);
		});
	}
	
</script>

 

后台处理:

 

@Controller
@RequestMapping("/blog")
public class BlogController {

    
    @RequestMapping("/{id}")
    public ModelAndView showBlog(@PathVariable("id") int id){
        ModelAndView mav = new ModelAndView();
        mav.addObject("id", id);
        mav.setViewName("blog");
        return mav;
    }
    
    /**
     * 带有参数的请求url @@RequestParam 是为了防止参数为空时报错
     * @param q
     * @return
     */
    @RequestMapping("/query")
    public ModelAndView showBlog2(@RequestParam(value="q",required=false) String q){
        ModelAndView mav = new ModelAndView();
        mav.addObject("q", q);
        mav.setViewName("query");
        return mav;
    }
    
}

 

posted @ 2018-09-06 22:13  Java开发小菜鸟  阅读(252)  评论(0编辑  收藏  举报