SpringMVC传递数据和对象

实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private int age;
}

Controller

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

@Controller
@RequestMapping("yonghu")//相当于分由
public class UserController {
    @RequestMapping("/user1")
    public String UserModel(@RequestParam("name") String name ,Model model){
        model.addAttribute("user",name);
        return "yonghu";
    }
@RequestMapping("/user")
public String UserNew(User user,Model model){
        model.addAttribute("user",user);
      return "yonghu";
}
}

@RequestMapping写在类上面相当于分由,写在方法上面相当于请求路径
上面例子请求地址相当于http://localhost:8080/yonghu/user

传递对象的时候一定要注意实体类的各个属性要保证一一对相应

你可能感兴趣的:(SSM,java,开发语言)