【问题标题】:id value does not seem to be captured correctly in servlet在 servlet 中似乎没有正确捕获 id 值
【发布时间】:2012-11-10 10:01:43
【问题描述】:

如果我在实现的 servlet 中任意分配一个“id”值,它就可以工作;但是,如果我没有正确地从抽象类继承 id 值并跳过“if”语句以转发到“then”语句中提供的 url。是什么赋予了?可以判断 'id' 变量有什么问题:

抽象 servlet 片段:

 protected Integer id = null;

 private void _doProcess(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException, ServletException {
    try {
        response.setContentType("text/html");
        writer = response.getWriter();
        final String idString = request.getParameter("id");
        if(StringUtil.isNotEmptyStringTrimmed(idString)){
            try {
                id = Integer.parseInt(idString.trim());
            } catch (NumberFormatException e) {
                id = null;
            }
        }

        doProcess(request,response);

    } finally {
        id = null;
    }

    try {
        writer.close();
    } catch (Exception e) {
        // no-op
    }
}

实现 servlet 片段:

 public void doProcess(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException, ServletException {

    // set page title
    final HttpSession session = request.getSession();
    session.setAttribute("pageTitle", "Training Project 5: Author");

    if (id == null){
        request.setAttribute("authorNamesList", printAuthorNames());
        request.getRequestDispatcher("authorList.jsp").include(request,response);
    }else{
        final Author author = BEAN_FACTORY.getMember(id);
        session.setAttribute("authorId",author.getId());
        session.setAttribute("name", author.getName());
        session.setAttribute("bio", author.getBio());
        session.setAttribute("picture",author.getPicture());
        session.setAttribute("bookTitles", printBookTitles(author.getId()));
        request.getRequestDispatcher("authorPage.jsp").include(request,response);
    }
}

当上面的 servlet 'else' 代码不在条件语句中时,下面的 jsp 代码可以工作:

 <div id="right">
   <table class="display" summary="Author Information">         
     <tr>
         <td><span class="brown">Author Id: <c:out value="${authorId}"/></span></td>
     </tr>
     <tr>
         <td><span class="brown">Name: <c:out value="${name}"/></span></td>
     </tr>
     <tr>
         <td><span class="brown">Bio: <c:out value="${bio}"/></span></td>
     </tr>
     <tr>
         <td>
             <p>
                 <span class="brown"><img src="<c:out value="${picture}"/>" alt= ""/></span>
             </p>
         </td>
     </tr>

【问题讨论】:

  • id 变量是什么?它在哪里定义?它的价值在哪里?
  • id 未定义吗?是否已初始化?
  • id 变量在这个 servlet 扩展的抽象类中定义:
  • 最终字符串 idString = request.getParameter("id"); if(StringUtil.isNotEmptyStringTrimmed(idString)){ 尝试 { id = Integer.parseInt(idString.trim()); } catch (NumberFormatException e) { id = null; } }
  • 请更新问题以反映id 变量。

标签: java model-view-controller jsp servlets jstl


【解决方案1】:

除非id 是一个类字段,否则您不会向我们展示所有代码。也不清楚你所说的“它不起作用”是什么意思。预期内容为空?你有例外吗?

【讨论】:

  • 我没有得到异常,它只是没有填充到会话中。为了检查,我打印了所有会话变量,但那里什么也没有。但是,如果我更改“id”的值并将“else”块的内容从 if/then 语句中取出,它会在我的 jsp 页面中正常打印。
  • 看起来可能是 id 的问题。 . .我只是将 if/then 语句保持不变,然后输入: id = 2 并打印得很好。奇怪的是,在不覆盖现有 id 值的情况下,程序流知道跳过 if 语句并转到 else .jsp 重定向。疯了。
【解决方案2】:

'Id' 将始终为 null 或未定义,而不是如果未初始化。

【讨论】:

    【解决方案3】:

    找出问题所在。 . .对不起,伙计们(和女孩)我没有给每个人足够的信息来解决这个问题。有一个 authorList.jsp 页面打印每个人都从中选择的列表(见下文)。用户在 authorList.jsp 页面中选择他们想要查看信息的作者,然后将作者 id 参数转发回 servlet,该 servlet 将用户转发到显示个人作者信息的 authorPage.jsp。

    基本上,我使用了以下 jsp 页面 (authorPage.jsp) 的确切 .jsp 名称,而不是 web.xml (listAuthor) 上的 servlet 映射名称。我有:

        <table summary="Author List">
            <c:forEach items="${authorNamesList}" var="name">
                <tr>
                   <td><span class="brown"><a href="authorPage.jsp?id=${name.key}">${name.value}</a></span></td>
                </tr>
            </c:forEach>
    

    应该是:

        <table summary="Author List">
            <c:forEach items="${authorNamesList}" var="name">
                <tr>
                   <td><span class="brown"><a href="listAuthor?id=${name.key}">${name.value}</a></span></td>
                </tr>
            </c:forEach>
        </table>
    

    有时最好先离开,然后再回来看看新鲜的!祝大家感恩节快乐:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-08
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多