0

我正在尝试使用 request.setAttribute 方法将对象传递给servelt,如下所示:

jsp:

<%request.setAttribute(ResponsibilitiesCollectionRdg.RESPONSIBILITIES_COLLECTION, new ResponsibilitiesCollectionRdg());%>

爪哇代码:

public class ResponsabilityHtmlCommand extends FrontCommand {


@Override
public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 
{

    int id = new Integer(request.getParameter("ID")); //get the id of the module
    boolean toAdd = new Boolean(request.getParameter("toAdd")); // get if we need to add or remove the responsibilities
    ResponsibilitiesCollectionRdg respos = (ResponsibilitiesCollectionRdg) request.getAttribute(ResponsibilitiesCollectionRdg.RESPONSIBILITIES_COLLECTION);
    //add or remove the responsibilities
    if(id != -1)
    {
        if(toAdd)
            respos.addResp(id);
        else
            respos.removeResp(id);
    }

    response.getWriter().write(ResponsabilityFinder.findHtmlForRespDropDown(respos.getListOfResp())); //send the tag
}

变量“respos”在 getAttribute 方法之后包含 null。任何想法如何解决我的问题?

4

1 回答 1

1

一旦 ajsp被处理,它就会被渲染为 html 并提交给HttpResponse OutputStream. jsp不再存在于 servlet 的上下文中,因此您不能以您可能认为的方式传递任何内容。

您可以做的是使参数可用于您发出的下一个 Http 请求,无论是从锚点还是在用作请求参数<a href="/my/wtv/site?attr=myattribute">的表单提交和输入元素中。<form action="/my/wtv/site?attr=myattribute">

于 2013-02-20T19:17:04.233 回答