SpringMVC的其他用法

使用RestFul风格的传参:

package com.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@Controller
public class HelloController {


    @RequestMapping(value = "/test/{a}/{b}",method = {RequestMethod.GET})
    public String testcontroller(@PathVariable("a") int a, @PathVariable("b") int b,Model model){

        int res=a+b;
        model.addAttribute("msg","结果是:"+res);
        return "test";
    }
}

跳转和重定向:

package com.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@Controller
public class HelloController {



    @RequestMapping(value = "/test/{a}/{b}",method = {RequestMethod.GET})
    public String testcontroller(@PathVariable("a") int a, @PathVariable("b") int b,Model model){

        int res=a+b;
        model.addAttribute("msg","结果是:"+res);
        return "test";//直接跳转的方式;
    }

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String testindex(Model model){

        model.addAttribute("msg","hello");
        return "redirect:/index.jsp";//重定向;url需要变化
    }
}

数据处理:设置@RequestParam("xxxx")属性

实体类:

SpringMVC的其他用法_第1张图片

package com.springmvc.controller;

import com.springmvc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Lenovo
 * @date 2023/3/26
 * @time 14:48
 * @project SpringMVC_2023_3
 **/

@Controller
public class UserController {


    @RequestMapping("/user1")
    //RequestParam :防止前端传入的参数与实际后台命名的参数不一致导致错误。
    public String testuser01(@RequestParam("username") String name, Model model){

        System.out.println("name:"+name);
        model.addAttribute("msg",name);
        return "user";
    }

    
    @RequestMapping("/user2")
    //RequestParam :防止前端传入的参数与实际后台命名的参数不一致导致错误。
    public String testuser02(@RequestParam("username") String name, ModelMap map){

        System.out.println("name:"+name);
        map.addAttribute("msg",name);
        return "user";
    }

    //传入一个对象:需要保证实体类的属性名,与前端传入的参数名要是一样的,否则属性值就是null
    @RequestMapping("/user3")
    public String testuser03(User user, Model model){

        System.out.println("user:"+user);
        model.addAttribute("user",user);
        return "user";
    }
   



}

后端设置了@RequestParam("username")

SpringMVC的其他用法_第2张图片

 如果传入的参数是name,与后端写的username不一致,就会报错误。

SpringMVC的其他用法_第3张图片

传入参数是一个实体对象:

正确的传参数名:

SpringMVC的其他用法_第4张图片

错误的传参数名:

SpringMVC的其他用法_第5张图片

你可能感兴趣的:(java,servlet,java,spring)