SpringMVC注解详情
-
路劲数组访问,多路径访问一个controller
- @RequestMapping(value=["/uri","/uri2"])
- @RequestMapping(path=["/uri","/uri2"])
-
限定请求方式只能为GET(其他方式同理,可以写成数组):
@RequestMapping(method=RequestMethod.GET)
@RequestMapping(path ={"p1","p2"},method= RequestMethod.GET) public String onlyget(){ System.out.println("this is onlygetMethod"); return "test"; }
获取项目的地址:
@WebServlet(urlPatterns = {},loadOnStartup = 2)
public class WebPatInitServlet extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
//在整个应用中上下文使用webpath来存储上下文路径
config.getServletContext().setAttribute("webpath",config.getServletContext().getContextPath());
super.init(config);
}
}
//loadonstartup =2 是为了让这个servlet最快加载,可以优先获取到webpath
表单访问:
<form action="${webpath}/test/p1" method="post" >
<input type="submit">
</form>
<!-- 以前的写法 ------->
<form action="${pageContext.request.contextPath}/test/p1" method="post" onsubmit="">
<input type="submit">
</form>
-
@ResponseBody (在请求页面返回数据)
@ResponseBody public String put(){ return "put"; }
-
@ModelAttribute
默认初始化信息:
若前端jsp有传值,则使用前端传入的值,否则则使用@ModelAttribute注解下的默认初始化值
@ModelAttribute public void init(Model model){ System.out.println("initing........."); User u=new User(); u.setUsername("linpr"); model.addAttribute("user",u); } @RequestMapping("p5") @ResponseBody public String MA(User user){ System.out.println(user.getUsername()); return user.getUsername(); }
<form action="${webpath}/test/p5" method="post" >
<input type="text" name="username">
<input type="submit">
</form>
-
@SessionAttributes
添加到类上面,在session中放置模型
@Controller @RequestMapping("/test") @SessionAttributes("user") public class TestController { @RequestMapping("p8") public String redirecttojsp(User user){ System.out.println("重定向"); return "redirect:/index.jsp"; } }
jsp中:
${sessionScope.user.username}
-
@SessionAttribute
登录的狗会被注册的狗给覆盖,只有进行了注册才能进行登录,且只能登录当前注册账号
@Controller @RequestMapping("/dog") @SessionAttributes("dog") public class DogController { @RequestMapping("/register") @ResponseBody public String register(Dog dog){ System.out.println("注册的狗名为:"+dog.getName()); return dog.getName(); } //会检查当前会话中是否含有Dog对象,如果,则会出现400错误 @RequestMapping("/login") public String login(@SessionAttribute Dog dog){ System.out.println(" 登录的名字"+dog.getName()); return "dog"; } }
-
@PathVariable注解
@RequestMapping("/restful/{id}/{name}")
public void test(@PathVariable("id")Integer id,@PathVariable("name")String name){
System.out.println("id为:"+id+"姓名为:"+name);
}
-
@RestController=@Controller+@Responsebody //当该Controller全是返回数据的方法时,可直接使用@RestController
-
@Requestbody
用来前台传递json数据到后台,使用方式
$('#b1').click(function () {
var obj={
"username":"周振昭",
"password":"goudongxi"
}
$.ajax({
url: '${webpath}/Json2/m1',
type: 'post',
contentType:'application/json',
data:JSON.stringify(obj),
success: function (data) {
}
}
)
})
@Controller
@RequestMapping("Json2")
public class JsonController2 {
@RequestMapping("m1")
public String t1(@RequestBody User user){
System.out.println(user.getUsername()+user.getPassword());
return null;
}
}
网友评论