gpt4 book ai didi

java - EL 是否需要 request.setAttribute ("...")?

转载 作者:行者123 更新时间:2023-11-29 05:29:55 25 4
gpt4 key购买 nike

如果我有一个包含以下代码的 bean:

private String method;

public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}

是否有必要:

request.setAttribute("method", method );

对于我希望从 JSP 中可见的每个变量?

最佳答案

如果 method 属性未在请求中设置,则评估的 ${method} 表达式在 jsp 中将为 null。如果您需要一些值,则必须将其设置为该值。

在你的 servlert 中做 post 方法;

public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException 
{
String formParameter = req.getParameter("someParameterName");
//Your logic dependen on form parameter

FormObject fo = new FormObject();
fo.setMethod("new value");
req.setAttribute("formObject", fo);
req.getRequestDispatcher("/WEB-INF/yourJspPage.jsp").forward(req, res);
}

你的java对象:

public class FormObject{
String method;

public String getMethod(){
return method;
}

public void setMethod(String method){
return this.method = method;
}
}

在你的 JspPage.jsp 中:

<div>${fo.method}</div>

附言我没有尝试过这个例子,但这个想法应该很清楚。你可以搜索 jsp + servlet tutorial 来了解你在做什么。有类似你想要的:enter link description here

session 类似于请求只是添加到这个对象的属性持续时间更长(不同的范围)。但我认为在为每个步骤寻求帮助之前,您应该阅读更多文档和教程。

关于java - EL 是否需要 request.setAttribute ("...")?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21480040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com