当我遇到障碍时,我正在更新一些相当旧且令人讨厌的代码。我的前任正在为 JSP 文件内的变量填充默认值(通过 scriptlet 和 Struts beans)。
有什么方法可以让我从 java 类(模型)访问 struts bean 的值,或者可以使用他的 bean 定义来引用该值并将其传递给我的 Java bean 吗?
(我是 bean 新手,所以要温柔)
以下是他当前检索默认值的方式
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<bean:define id="SummaryNumbers" name="SummaryNumbers" type="QueryResults"/>
这是我访问模型类的方式
<jsp:useBean id="content" class="com.xxx.xxx.ProductionContent">
<jsp:setProperty name="content" property="summaryNumbers" />
</jsp:useBean>
最佳答案
我相信您正在使用 Struts 1,并且有更好的方法可以做到这一点。在您的 Struts 操作类中
public class ProductionAction extends DispatchAction {
public ActionForward showProductionConent(ActionMapping mapping, ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws STException
{
//.. get productionContent here
request.setAttribute("productionContent",productionContent);
return mapping.findForward("showProductionContent");
}
}
在你的 JSP 中 您可以使用 EL 来检索这样的数据
<td>Production Content Name: ${productionContent.name}</td>
或者您可以使用struts html标签来检索这样的数据(您需要在struts-config.xml中正确绑定(bind)form bean)
<td>Production Content Name: <html:text property="name"></td>
关于java - 从Java类访问struts bean的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4514358/