Java SpringMVC

MVC
M:Model
V:View
C:Controller - servlet/action/controller

servlet中是用servlet做Controller,struts中是用action做Controller,而在SpringMVC中是用controller做Controller。

路由

url描述
@RequestMapping(“list”)
@RequestMapping(“/list.do”)如果在web.xml中配置了servlet-mapping则不需要再重新配置
@RequestMapping(value = “/list3”,method=RequestMethod.POST)只能使用POST方法
@RequestMapping(value = “/list3”,method=RequestMethod.Get)只能使用GET方法

在执行访问方法(get或post)时需要用到“value”和“method”参数。

例子:

@Controller//声明该类用注释
@RequestMapping("/user")//访问类的路径
public class UserController{
    @RequestMapping("/toRegister")//访问方法的路径
    public String toRegister(){
        return "user/register";//返回视图jsp文件,在DispatcherServlet-servlet.xml已经配置了文件的前缀和后缀
    }
}

接收请求参数

Controller//声明该类用注释
@RequestMapping("/user")//访问类的路径
public class UserController{

    1 访问页面
    @RequestMapping("/register")//访问方法的路径

    public String toRegister(){

        return "user/register";//返回注册页面
    }

    // 转发
    @RequestMapping("/forward")
    public String froward(){
        //同一个控制器转发
        return "forward:register.do";
    }

    // 重定向
    @RequestMapping("/redirect")
    public String redirect(){
        //同一个控制器转发
        return "redirect:register.do";
    }


    2 get方法传参
    //form表单的get请求和在url中拼贴? &是等价的,下面的演示都适用url
    //1.普通get方法,既? &类型
    @RequestMapping("/get")
    public String getMethod(Integer uid, Model model){
        System.out.println(uid);
        model.addAttribute("uid",uid);
        return "user/getview";
    }
    //2.restfull风格get方法.所谓restfull风格就是将参数放在url的斜杠里面
    @RequestMapping("/getrest/{uid}")
    public String getMethodRest(@PathVariable Integer uid, Model model){
        System.out.println(uid);
        model.addAttribute("uid",uid);
        return "user/getrestview";
    }


    3 post方法传参
    //1.获取表单参数方法:将表单提交的参数写入controller方法的参数里
    @RequestMapping(value ="/login",method = RequestMethod.POST)
    public String register(String username,String password,
                           int age,String gender,Date birthday,
                           String[] hobbyIds){

        System.out.println(username);
        System.out.println(password);
        System.out.println(age);
        System.out.println(gender);
        System.out.println(birthday);
        System.out.println(Arrays.toString(hobbyIds));
        return "user/info";
    }


    //2.获取表单参数方法:将表单提交的参数通过model获取
    @RequestMapping(value ="/login2",method = RequestMethod.POST)
    public String register2(User user, Model model){
        System.out.println(user);
        model.addAttribute("user",user);
        return "user/info";
    }



    json
    //1.普通方式处理
    @RequestMapping("/registerjson1")
    public String hello(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //获得json格式数据
        StringBuffer jb = new StringBuffer();
        String line;
        BufferedReader reader = request.getReader();
        while ((line = reader.readLine()) != null)
            jb.append(line);
        //返回json格式数据
        PrintWriter out = response.getWriter();
        out.append(jb.toString());
        return null;
    }



    //2.包装好的json类
    @RequestMapping("/registerjson2")
    public @ResponseBody UserJson registerjson(@RequestBody UserJson userJson){
        return userJson;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值