java javax.servlet.jsp.JspException:无法在任何范围内找到 bean:“CommentsUpdated”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26900012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.servlet.jsp.JspException: Cannot find bean: "CommentsUpdated" in any scope
提问by Amos
Struts-config.xml:
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:
瓷砖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:
通用布局.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:
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:
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");
}
}
I managed to create the template and call in the forms without the "name" attributes.It worked, then i realized i have to call in forms that have names attribute(CommentsUpdated
) so that i able to pass the variables for digging the data from database.Unluckily it throws error like this:
我设法创建模板并调用没有“名称”属性的表单。它起作用了,然后我意识到我必须调用具有名称属性(CommentsUpdated
)的表单,以便我能够传递用于从数据库中挖掘数据的变量. 不幸的是它会抛出这样的错误:
javax.servlet.jsp.JspException: Cannot find bean: "CommentsUpdated" in any scope
.
javax.servlet.jsp.JspException: Cannot find bean: "CommentsUpdated" in any scope
.
i understand that the name attribute in my serachMyUser.jsp
causing this error, but i'm stuck on this part, i've tried to search in google for examples, but all i get is the forms were called in without names attribute in it.
我知道我的名称属性serachMyUser.jsp
导致了这个错误,但我被困在这部分,我试图在谷歌中搜索示例,但我得到的只是调用的表单没有名称属性。
回答by Amos
<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"/>
finally i found the Problem, after i enter name="CommentsUpdated"
inside the activity in struts-config.xml..it works! hope this might helpful for others too!
最后我找到了问题,在我进入name="CommentsUpdated"
struts-config.xml 中的活动后..它工作了!希望这对其他人也有帮助!
回答by Roman C
If you didn't set the name
property in the action config, which should be the same as <form-bean>
name, but used the name in the tag
如果你没有name
在action config中设置属性,应该和<form-bean>
name一样,但是在标签中使用了name
<html:text name="CommentsUpdated" .../>
of course you got an exception
当然你有例外
Cannot find bean: “CommentsUpdated” in any scope
无法在任何范围内找到 bean:“CommentsUpdated”
Struts will instantiate a form-bean and pass it to the action if the form bean and action name are configured. If you want to instantiate it yourself then you should put it into any scope before accessing it in JSP.
如果配置了表单 bean 和操作名称,Struts 将实例化表单 bean 并将其传递给操作。如果你想自己实例化它,那么你应该在 JSP 中访问它之前将它放入任何范围。