配置视图解析器
package com.kute.controller; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.order.domain.AllOrderInfoBean; import com.order.util.JsonUtils; import com.tgb.entity.User; @Controller @RequestMapping("/test") //这里也可以写成@RequestMapping("/test.do")也可以不写,因为在web.xml中配置了*.do public class TestController { @RequestMapping(params="method=addUser") public String addUser(User user){ System.out.println("dddddddddd"); return "success"; } @RequestMapping(value="/test0")//默认为get方式 public String test0(){ System.out.println("访问路径:test/test0.do"); return "success"; } @RequestMapping(value="/test1",method=RequestMethod.POST) public String test1(String sex){ System.out.println(sex); System.out.println("访问路径为:test/test1.do,而且是get方式______"+sex); return "success"; } @RequestMapping(value="/test2",method=RequestMethod.GET,params="param=test2") public String test2(){ System.out.println("访问路径为:test/test1.do?param=test2,而且是get方式"); return "success"; } //REST风格的参数 @RequestMapping(value="/test3/{name}") public String test3(@PathVariable String name){//形参和接收参数的名称一致 System.out.println(name); System.out.println("访问路径为:test/test3/zhangsan.do"); System.out.println("看这里的访问路径,直接就将‘zhangsan’这个参数值就传递给了@RequestMapping(value=\"/test3/{name}\")中的name参数名,可随意命名参数名称,"); return "success"; } @RequestMapping(value="/test5/{name}") public String test5(@PathVariable("name")String rename){//形参和接收参数的名称不一致 System.out.println(rename); System.out.println("访问路径为:test/test5/zhangsan.do"); System.out.println("看这里的访问路径,直接就将‘zhangsan’这个参数值就传递给了@RequestMapping(value=\"/test5/{name}\")中的name参数名,可随意命名参数名称," + "然后后面的@PathVariable(\"name\")中的name名称要和上面的那个name保持一致,然后把此name绑定到形参rename"); return "success"; } @RequestMapping(value="/test4/{sex}/{sno}") //这里我写成@RequestMapping(value="test4/{sex}/{sno}")也是可以滴 public String test4(@PathVariable("sex")String sex,@PathVariable("sno")String sno){ System.out.println(sex); System.err.println(sno); System.out.println("访问路径:test/test4/male/10506.do"); return "success"; } //支持正则 @RequestMapping(value="/test6/{textualPart:[a-z-]+}.{numericPart:[\\d]+}") public String test6(@PathVariable String textualPart,@PathVariable String numericPart){ System.out.println(textualPart);//输出sometxt System.out.println(numericPart);//输出123 System.out.println("访问路径:test/test6/sometxt.123.do"); return "success"; } //访问WEB-INF下的views文件中的不同目录的页面,这里访问的是user目录下的,不知道其他有什么好的办法没 @RequestMapping(value="/test7") public String test7(){ System.out.println("访问路径:test/test7.do"); return "user/success"; } //重定向到另一个controller @RequestMapping(value="/test8") public String test8(){ System.out.println("访问路径:test/test8.do"); return "redirect:/OrderInfo/queryOrderInfo.do"; } //随时添加所需参数 @RequestMapping(value="/test9") public String test9(HttpServletResponse response){ System.out.println("访问路径:test/test9.do,这里我需要一个response,所以直接在形参上写上即可"); return "user/success"; } //ajax:添加@ResponseBody注解 @RequestMapping(method = RequestMethod.GET, value = "/test10") public @ResponseBody String getEndTimeFBeginTime(@PathVariable("roomId") String roomId, @PathVariable("dtime") String dtime, @PathVariable("beginTime") String beginTime, ModelMap model) { List<AllOrderInfoBean> endList = userService.getEndTimeFBeginTime( roomId, dtime, beginTime); model.put("endList", endList);//这里的model相当于session return JsonUtils.toJson(endList);//ajax返回的数据 } }
最后复制下原理:原文:http://blog.csdn.net/xtu_xiaoxin/article/details/8796499