Springmvc 注解学习

2019-01-01  本文已影响0人  Zak1

SpringMVC注解详情

获取项目的地址:

@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>
<form action="${webpath}/test/p5" method="post" >
    <input type="text" name="username">
    <input type="submit">
</form>
${sessionScope.user.username}
 @RequestMapping("/restful/{id}/{name}")
    public void test(@PathVariable("id")Integer id,@PathVariable("name")String name){
        System.out.println("id为:"+id+"姓名为:"+name);

    }

  $('#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;
      }
  }
上一篇 下一篇

猜你喜欢

热点阅读