【问题标题】:Can model class implement Model UI?模型类可以实现模型UI吗?
【发布时间】:2020-09-14 17:26:46
【问题描述】:

到目前为止,在我使用 Spring Boot 的 Java 代码中,我使用模型或 POJO 对象来更好地控制我的对象等。通常我正在创建实体、存储库、服务、Rest 控制器,就像文档和课程所建议的那样. 但是现在我正在使用 Thymeleaf 模板、HTML 一些 Bootstrap 和 CSS 来创建浏览器界面。对于@Controller 中的方法,作为参数,我从 Spring Model UI 传递 Model,如下所示:

@GetMapping("/employees")
private String viewAllEmployees(Model employeeModel) {
    employeeModel.addAttribute("listEmployees", employeeService.getAllEmployees());
    return "employeeList";
}

我的问题是:如何使用我的 POJO 对象而不是 org.springframework.ui.Model;?

我的第一个猜测是:

public class EmployeeModel implements Model{

private long employeeId;
private String firstName;
private String lastName;
private String email;
private String phone;
private long companyId;
//getter and setter methods
}

为了做到这一点,我必须 @Override 模型方法,这对我来说很好。看起来 Java、Spring 等在编译时不会抱怨,我可以在我的 @Controller 中使用这个 POJO 对象,如下所示:

@Controller
public class EmployeeController {
@Autowired 
private EmployeeService employeeService;    
@GetMapping("/employees")
private String viewAllEmployees(EmployeeModel employeeModel) {
    employeeModel.addAttribute("listEmployees", employeeService.getAllEmployees());
    return "employeeList";
}}

我运行代码并启动,显示我的 /home 端点,它很酷,但是当我想去我的 /employees 端点时,它应该显示我的员工列表,它会抛出这个:

Method [private java.lang.String com.bojan.thyme.thymeApp.controller.EmployeeController.viewAllEmployees(com.bojan.thyme.thymeApp.model.EmployeeModel)] with argument values:[0] [type=org.springframework.validation.support.BindingAwareModelMap] [value={}] ] with root cause java.lang.IllegalArgumentException: argument type mismatch

异常。

请注意,Rest 控制器在浏览器和 Postman 中运行良好。

String 作为方法是否可能是问题所在?我的方法应该是其他类型,例如 List<EmployeeModel> 还是 EmployeeModel 本身?如果是这样,如何告诉方法我希望返回我的employeeList.html?

我真诚地希望有人能用这个来阻止我:)

【问题讨论】:

    标签: java spring spring-boot spring-mvc thymeleaf


    【解决方案1】:

    如何使用我的 POJO 对象而不是 org.springframework.ui.Model;?

    当您使用 Thymeleaf 时,我认为这不是最佳做法。根据他们的documentation,您应该将您的对象附加到您的模型。因此,在您的控制器中,您将操纵包含您的 Pojos 的模型。

    例子:

     @RequestMapping(value = "message", method = RequestMethod.GET)
        public ModelAndView messages() {
            ModelAndView mav = new ModelAndView("message/list");
            mav.addObject("messages", messageRepository.findAll());
            return mav;
        }
    

    【讨论】:

      【解决方案2】:

      您应该始终使用org.springframework.ui.Model 作为参数。这个类基本上是一个Map,带有可供 Thymeleaf 用于渲染的键/值对。

      你的第一个例子是你应该怎么做:

      @GetMapping("/employees") //<1>
      private String viewAllEmployees(Model model) {
          model.addAttribute("employees", employeeService.getAllEmployees()); // <2>
          return "employeeList"; // <3>
      }
      
      • 这是视图将在其上呈现的 URL
      • 将所需的任何 Java 对象作为属性添加到模型中
      • 返回 Thymeleaf 模板的名称。在带有 Thymeleaf 应用程序的默认 Spring Boot 中,这将引用位于 src/main/resources/templates/employeeList.html 的模板。在该模板中,您将能够使用 ${employees} 访问您的模型值。

      【讨论】:

      • 谢谢。这是有道理的。所以没有办法在 Thymeleaf 中使用 POJO 对象,我必须使用 Entities。
      • @TavernaJoe 不确定您的确切意思。您可以将任何类型的对象作为属性添加到Model。因此,您可以从 employeeService 返回 POJO,或者您可以将 Employee 实体转换为控制器中的 EmployeeDto
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 2011-02-09
      • 1970-01-01
      • 2022-05-26
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多