【问题标题】:javax.servlet.jsp.JspException: Cannot find bean: "CommentsUpdated" in any scopejavax.servlet.jsp.JspException:在任何范围内找不到 bean:“CommentsUpdated”
【发布时间】:2014-11-13 01:49:57
【问题描述】:

Struts-config.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
      "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
  <form-beans>
  </form-beans>

  <global-exceptions>
  </global-exceptions>

<global-forwards>
    <forward name="welcome"  path="/Welcome.do"/>
</global-forwards>

<action-mappings>
    <action path="/Link" parameter="method" type="com.sicmsb.action.LinkAction">
        <forward name="search" path="search"/>
    </action>
    <action path="/Welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>

<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

<message-resources parameter="com/vaannila/ApplicationResource"/>

<plug-in className="org.apache.struts.tiles.TilesPlugin" >
    <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />      
    <set-property property="moduleAware" value="true" />
</plug-in>

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property
        property="pathnames"
        value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

</struts-config>

tiles-defs.xml:

<?xml version="1.0" encoding="UTF-8" ?>

 <!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
       "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
<tiles-definitions>
    <definition name="baseLayout" path="/common-layout.jsp">
        <put name="header" value="/header.jsp" />
        <put name="body" value="/body.jsp" />
        <put name="footer" value="/footer.jsp" />
    </definition>
    <definition name="search" extends="baseLayout">

        <put name="body" value="/searchMyUser.jsp" />
    </definition>
</tiles-definitions>

common-layout.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title></title>
</head>
<body>
     <table border="1"  width="100%" height="100%">
        <tr>
            <td><tiles:insert attribute="header" /></td>
        </tr>

        <tr>
            <td><tiles:insert attribute="body" /></td>
        </tr>

        <tr>
            <td><tiles:insert attribute="footer" /></td>
        </tr>
    </table>
</body>
</html>

searchMyUser.jsp:

<%@page import="com.sicmsb.bean.Comments"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>

<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<script>
    function submitAdd(form) {
        form.submit();
    }

    function goDetail(commentId) {
        var myform = document['detForm'];
        //myform.action = "/detail.do";
        myform['commentId'].value = commentId;
        myform.submit();
    }
</script>
<style>
A {
    text-decoration: underline;
    color: blue;
    cursor: pointer;
}
</style>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Search here</title>
</head>
<body>
    <form name="detForm" action="details.do" method="post">
        <input type="hidden" name="commentId" />
    </form>

    <h1>Search in MyUser</h1>

    <form action="SearchUser.do" method="post">
        <table cellpadding="3pt">
            <tr>
                <td>Insert Name : <html:text name="CommentsUpdated"
                        property="myUser" size="30"></html:text></td>
            </tr>
        </table>
        <p />
        <input type="button" value="Search"
            onclick="this.disabled=true; submitAdd(this.form)" />
    </form>

    <hr />

    <table>
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Webpage</th>
            <th>Summary</th>
            <th>Comments</th>
        </tr>

        <logic:iterate id="comment" name="listComments"
            type="com.sicmsb.bean.Comments">
            <tr>
                <td><a onclick="goDetail('<%=comment.getCommentId()%>')"> <bean:write
                            name="comment" property="myuser" />
                </a>
                <td>|<bean:write name="comment" property="email" />
                <td>|<bean:write name="comment" property="webpage" />
                <td>|<bean:write name="comment" property="summary" />
                <td>|<bean:write name="comment" property="comments" />
            </tr>
        </logic:iterate>
    </table>
</body>
</html>

LinkAction.java:

package com.sicmsb.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class LinkAction extends DispatchAction {
    public ActionForward Search(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        return mapping.findForward("search");
    }
}

我设法创建模板并在没有“名称”属性的情况下调用表单。它起作用了,然后我意识到我必须调用具有名称属性的表单(CommentsUpdated),以便我能够传递变量用于从数据库中挖掘数据。不幸的是,它会引发如下错误:

javax.servlet.jsp.JspException: Cannot find bean: "CommentsUpdated" in any scope.

我知道我的serachMyUser.jsp 中的 name 属性导致了这个错误,但我被困在这部分,我试图在谷歌中搜索示例,但我得到的只是表单被调用但没有名字属性。

【问题讨论】:

  • 究竟是什么问题?
  • 当我在 tomcat 服务器中加载它时,它会抛出如下错误:'javax.servlet.jsp.JspException: 找不到 bean: "CommentsUpdated"' @bencripps
  • 你没有定义表单bean,我不确定你期望什么。
  • 无关,但这必须来自一个非常老的教程;太可怕了。
  • 最后我找到了问题,在我在 struts-config.xml 中的活动中输入 name="CommentsUpdated" 之后......它有效!希望这对其他人也有帮助!

标签: java struts-1


【解决方案1】:
<action path="/Link" parameter="method" type="com.sicmsb.action.LinkAction" name="CommentsUpdated">
            <forward name="baseLayout" path="baseLayout"/>
            <forward name="search" path="search"/>
            <forward name="details" path="details"/>
            <forward name="insert" path="insert"/>

最后我发现了问题,在我在 struts-config.xml 的活动中输入name="CommentsUpdated" 之后......它可以工作!希望这对其他人也有帮助!

【讨论】:

    【解决方案2】:

    如果你没有在action config中设置name属性,它应该和&lt;form-bean&gt;名字一样,但是在标签中使用了名字

    <html:text name="CommentsUpdated" .../>
    

    当然你有一个例外

    在任何范围内都找不到 bean:“CommentsUpdated”

    如果表单 bean 和动作名称已配置,Struts 将实例化一个表单 bean 并将其传递给动作。如果你想自己实例化它,那么你应该在 JSP 中访问它之前将它放入任何范围。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 2021-06-18
      • 2021-01-15
      相关资源
      最近更新 更多