model.addAttribute()和request.setAttribute()有什么区别?

request.setAttribute()

属于页面之间的传值,从a.jsp到b.jsp一次传递,之后这个request就会失去他的作用范围,再传就要重新设一个request.setAttribute()。(使用session.setAttribute()会在一个过程中始终保持这个值)

注:javascript与jsp中不能相互传值,因为javascript运行在客户端,jsp运行在服务器端。若想使他们能够相互传参数,可以在jsp页面中设置一个hidden属性的控件,用它的value来传递所需的数值。

1、把js直接写在了JSP页面,所以能获取到。如果单独把JS抽离出来是不可能获取到的。前者服务端解析能或许到,后者客户端解析获取不到。

2、js里是在客户端执行的,不能直接获取,因为EL表达式是在服务器的时候被解析的,所以可以获取到。一个是客户端的东西,一个是服务器端的东西。

3、jsp能取到,那是因为他本身就是servlet。js不可能有方法直接获取到HttpServletRequest里面的属性值。

4、setAttribute是服务器行为,到了客户端就无效了,也谈不上如何用。除非你在jsp的时候就写到js变量里,或者使用ajax请求你需要的数据。

下面是 setAttribute()的源码以及注解:

/**
     * Stores an attribute in this request.
     * Attributes are reset between requests.  This method is most
     * often used in conjunction with {@link RequestDispatcher}.
     *
     * <p>Attribute names should follow the same conventions as
     * package names. Names beginning with <code>java.*</code>,
     * <code>javax.*</code>, and <code>com.sun.*</code>, are
     * reserved for use by Sun Microsystems.
     *<br> If the object passed in is null, the effect is the same as
     * calling {@link #removeAttribute}.
     * <br> It is warned that when the request is dispatched from the
     * servlet resides in a different web application by
     * <code>RequestDispatcher</code>, the object set by this method
     * may not be correctly retrieved in the caller servlet.
     *
     * @param name a <code>String</code> specifying 
     * the name of the attribute
     *
     * @param o the <code>Object</code> to be stored
     *
     */
    public void setAttribute(String name, Object o);

model.addAttribute()

该方法的作用跟request.setAttribute()的本质一样,就是为了给JSP页面传值。

下面是addAttribute()的源码以及注解:

   /**
	 * Add the supplied attribute under the supplied name.
	 * @param attributeName the name of the model attribute (never {@code null})
	 * @param attributeValue the model attribute value (can be {@code null})
	 */
	Model addAttribute(String attributeName, Object attributeValue);

其他文章里面有提到,这两个方法的输出的值不一样,可参考:

https://blog.csdn.net/csdn1115698735/article/details/82426553

也有提到两个方法设置的值的数量也不一样,可参考:

https://blog.csdn.net/qq_32187411/article/details/53735240

 

 

  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个 Spring MVC 的 Controller 类,其中: 1. `@Controller` 注解表示该类是一个控制器。 2. `@RequestMapping("/orders")` 注解表示该控制器处理的请求的 URL 前缀为 "/orders"。 3. `@Autowired` 注解表示自动注入 HttpServletRequest 和 OrderService 对象。 4. `@PostMapping("/order")` 注解表示该方法处理 HTTP POST 请求,并且请求的 URL 为 "/orders/order"。该方法的参数列表中包含了一个 List<Cart> 类型的 carts 对象和一个 Model 类型的 model 对象,表示从前端页面传来的购物车列表和模型数据。 5. `@GetMapping("/order2")` 注解表示该方法处理 HTTP GET 请求,并且请求的 URL 为 "/orders/order2"。该方法的参数列表中包含了一个 Model 类型的 model 对象,表示模型数据。 6. `@GetMapping("/ok")` 注解表示该方法处理 HTTP GET 请求,并且请求的 URL 为 "/orders/ok"。 7. `HttpSession session = request.getSession()` 表示获取当前请求的 HttpSession 对象。 8. `Long userId =(Long)session.getAttribute("userId")` 表示从 HttpSession 中获取 userId 属性值,并将其转换为 Long 类型。 9. `List<Order> orders = orderService.getByUserId(userId)` 表示调用 orderService 的 getByUserId 方法,根据用户 id 获取订单列表。 10. `session.setAttribute("orders",orders)` 和 `session.setAttribute("carts",carts)` 表示将订单列表和购物车列表存入 HttpSession 中。 11. `model.addAttribute("username",session.getAttribute("username"))`、`model.addAttribute("orders",orders)` 和 `model.addAttribute("carts",carts)` 表示将 HttpSession 中的属性值存入 Model 对象中,以便在前端页面中使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值