一、@Controller

  • @Controller 标记一个类是Controller

二、RequestMapping  地址映射

  • 2.1  Value的操作。

注解在类上面    (父)
                注解在方法上面。 (子)

  • 2.1.1 SpringMVC完整的请求,就是父子加请来的请求。

        <a href="<%=request.getContextPath()%>/userController/list">list方法</a>
<a href="<%=request.getContextPath()%>/userController/save">save方法</a>

controller:

package com.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value = "/userController")
public class UserController {
@RequestMapping(value = "/list")
public ModelAndView list(HttpServletRequest request,
HttpServletResponse response) throws Exception { System.out.println("r执行list方法"); ModelAndView mv = new ModelAndView();
mv.addObject("req_key", "Model Data");
mv.setViewName("succ");
return mv;
} @RequestMapping(value = "/save")
public ModelAndView save(HttpServletRequest request,
HttpServletResponse response) throws Exception { System.out.println("r执行save方法"); ModelAndView mv = new ModelAndView();
mv.addObject("req_key", "Model Data");
mv.setViewName("succ");
return mv;
} }
  • 2.2   用于映射请求中的参数。

        <a href="<%=request.getContextPath()%>/paramController?task=list">list方法</a>
<a href="<%=request.getContextPath()%>/paramController?task=save">save方法</a>

controller:

package com.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value = "/paramController")
public class ParamController {
@RequestMapping(params = "task=list")
public ModelAndView list(HttpServletRequest request,
HttpServletResponse response) throws Exception { System.out.println("r执行list方法"); ModelAndView mv = new ModelAndView();
mv.addObject("req_key", "Model Data");
mv.setViewName("succ");
return mv;
} @RequestMapping(params = "task=save")
public ModelAndView save(HttpServletRequest request,
HttpServletResponse response) throws Exception { System.out.println("r执行save方法"); ModelAndView mv = new ModelAndView();
mv.addObject("req_key", "Model Data");
mv.setViewName("succ");
return mv;
} }
  •   2.3  Method:表示是处理哪一种请求.GET/POST

     <a href="<%=request.getContextPath()%>/methodController/add">list方法</a>

        <form method="post" action="<%=request.getContextPath()%>/methodController/add">
<input type="submit" value="提交"/>
</form>

controller:

package com.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value = "/methodController")
public class MethodController { @RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception { System.out.println("处理GET请求"); ModelAndView mv = new ModelAndView();
mv.addObject("req_key", "Model Data");
mv.setViewName("succ");
return mv;
} @RequestMapping(value = "/add", method = RequestMethod.POST)
// @RequestMapping(value = "/add", method = { RequestMethod.GET,
// RequestMethod.POST })
public ModelAndView save(HttpServletRequest request,
HttpServletResponse response) throws Exception { System.out.println("处理POST请求"); ModelAndView mv = new ModelAndView();
mv.addObject("req_key", "Model Data");
mv.setViewName("succ");
return mv;
} }
  •   2.4  RequestParam:获取地址中的参数信息。

    <a href="<%=request.getContextPath()%>/requestParamController/edit?userid=1">list方法</a>
<a href="<%=request.getContextPath()%>/requestParamController?task=edit&userid=2">list方法</a>

controller

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value = "/requestParamController")
public class RequestParamController { @RequestMapping(value = "/edit", method = RequestMethod.GET)
public ModelAndView edit_1(
@RequestParam(name = "userid", required = false) String id)
throws Exception { System.out.println("userid = " + id); return null;
} @RequestMapping(params = "task=edit", method = RequestMethod.GET)
public ModelAndView edit_2(String userid) throws Exception { System.out.println("task=edit中的userid = " + userid); return null;
} }
  •   2.5  PathVariable:REST风格.

    <a href="<%=request.getContextPath()%>/pathVarController/edit/1/admin">edit方法</a>
<a href="<%=request.getContextPath()%>/pathVarController/edit/2/guest">edit方法</a>
<a href="<%=request.getContextPath()%>/pathVarController/edit/3/test">edit方法</a> <a href="<%=request.getContextPath()%>/pathVarController/3/admin/delete">通配符一个方法</a>
<a href="<%=request.getContextPath()%>/pathVarController/3/admin/test/xxxx/yyyy/remove">通配符多个方法</a>

  controller

package com.controller;

import org.springframework.stereotype.Controller;
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.servlet.ModelAndView; @Controller
@RequestMapping(value = "/pathVarController")
public class PathVarController { @RequestMapping(value = "/edit/{userid}/{username}", method = RequestMethod.GET)
public ModelAndView edit_1(@PathVariable(value = "userid") String userid,
@PathVariable(value = "username") String username) throws Exception { System.out.println("userid = " + userid);
System.out.println("username = " + username); return null;
} /**
* 通配符的写法
*
* 1个*代表通配一个地址
*
* 2个*代表通配多个地址
*/
@RequestMapping(value = "{userid}/*/delete", method = RequestMethod.GET)
public ModelAndView delete_1(@PathVariable(value = "userid") String userid)
throws Exception { System.out.println("delete_1====>userid = " + userid); return null;
} @RequestMapping(value = "{userid}/**/remove", method = RequestMethod.GET)
public ModelAndView delete_2(@PathVariable(value = "userid") String userid)
throws Exception { System.out.println("remove_2====>userid = " + userid); return null;
}
}

  2.6  header和cookie

<a href="<%=request.getContextPath()%>/headerController/header">header</a>

<a href="<%=request.getContextPath()%>/headerController/cookie">cookie</a>

controller

package com.shyroke.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value = "/headerController")
public class HeaderController { @RequestMapping(value = "/header", method = RequestMethod.GET)
public ModelAndView header(@RequestHeader(name = "Host") String Host,
@RequestHeader(name = "User-Agent") String agent, HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("header");
System.out.println("Host= " + Host);
System.out.println("agent= " + agent); System.out.println("RequestHeader = " + request.getHeader("Host")); Cookie cookie_a = new Cookie("cookie_a", "Admin/GTUeST/axxxx"); cookie_a.setPath("/");
response.addCookie(cookie_a);
return null;
} @RequestMapping(value = "/cookie", method = RequestMethod.GET)
public ModelAndView cookie(
@CookieValue(name = "JSESSIONID", required = false) String sessionID,
@CookieValue(name = "cookie_a", required = false) String cookie_a)
throws Exception {
System.out.println("cookie_1 = " + sessionID); System.out.println("cookie_a = " + cookie_a); return null;
} }

结果

(三)springmvc之注解的基本使用的更多相关文章

  1. 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s

     1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2  spring-mv ...

  2. springMVC的注解详解

    springmvc常用注解标签详解 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业 ...

  3. springmvc常用注解与类型转换

    springmvc常用注解与类型转换 一:前置 spring -servlet.xml 注入 <!-- 启用spring mvc 注解 --> <context:annotation ...

  4. springMvc的注解注入方式

    springMvc的注解注入方式 最近在看springMvc的源码,看到了该框架的注入注解的部分觉的有点吃力,可能还是对注解的方面的知识还认识的不够深刻,所以特意去学习注解方面的知识.由于本人也是抱着 ...

  5. 一 : springmvc常用注解

    springmvc常用注解详解1.@Controller在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层 ...

  6. 转:springmvc常用注解标签详解

    Spring5:@Autowired注解.@Resource注解和@Service注解 - IT·达人 - 博客园--这篇顺序渐进,讲得超级好--此人博客很不错http://www.cnblogs.c ...

  7. springBoot注解大全JPA注解springMVC相关注解全局异常处理

    https://www.cnblogs.com/tanwei81/p/6814022.html 一.注解(annotations)列表 @SpringBootApplication:包含了@Compo ...

  8. SpringMVC 常用注解 详解

    SpringMVC 常用注解 详解 SpringMVC 常用注解 1.@RequestMapping                                      路径映射 2.@Requ ...

  9. SpringMVC常用注解實例詳解3:@ResponseBody

    我的開發環境框架:        springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...

  10. SpringMVC常用注解實例詳解2:@ModelAttribute

    我的開發環境框架:        springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...

随机推荐

  1. 梯度下降法(BGD & SGD & Mini-batch SGD)

    梯度下降法(Gradient Descent) 优化思想:用当前位置的负梯度方向作为搜索方向,亦即为当前位置下降最快的方向,也称“最速下降法”.越接近目标值时,步长越小,下降越慢. 如下图所示,梯度下 ...

  2. STM32 + RT Thread OS 串口通讯

    1.   创建项目 a)   禁用Finsh和console b)   默认情况下,项目文件包含了finsh,它使用COM1来通讯,另外,console输出(rt_kprintf)也使用了COM1.因 ...

  3. OpenJudge计算概论-奥运奖牌计数

    /*===================================================================== 奥运奖牌计数 总时间限制: 1000ms 内存限制: 6 ...

  4. Java多线程中thread.getname()和thread.currentThread().getName();的去别

    首先要明白 this.XXX 的使用场景 使用Thread.currentThread().getName()和使用this.getName()和对象实例.getName(),都可以得到线程的名称,但 ...

  5. java大文件下载+断点续传

    java两台服务器之间,大文件上传(续传),采用了Socket通信机制以及JavaIO流两个技术点,具体思路如下: 实现思路:1.服:利用ServerSocket搭建服务器,开启相应端口,进行长连接操 ...

  6. pytorch0.4.1安装

    pytorch官网:https://pytorch.org/ 这里安装pytorch0.4.1版本(最新版本为1.3.0系列,但是在跑github上的一些项目时会不断地报“ UseWarinig:Le ...

  7. videojs调整音频播放语速

    参考来源: https://stackoverflow.com/questions/19112255/change-the-video-playback-speed-using-video-js 以下 ...

  8. (十一)使用Jconsole监控线程

    一.案例 监控线程情况,包括阻塞.死循环等 1.1 代码如下,下述代码共有三个线程,Main.mythread01.mythread02线程,其中mythread01线程为死循环.mythread02 ...

  9. 【Leetcode_easy】674. Longest Continuous Increasing Subsequence

    problem 674. Longest Continuous Increasing Subsequence solution class Solution { public: int findLen ...

  10. iOS算法题

    1兔子算法题 兔子可以跳一步2步或者3步,问跳到100有多少种跳法? // 兔子可以跳一步2步或者3步 // 问跳到100有几种跳法 /* 分析1 两个变量,X*2+Y*3=100. X最大为50,X ...