<form-beans>
<form-bean name="stuForm" type="my.form.StuForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards>
<forward name="myForward" path="/MyJsp.jsp"/>
</global-forwards>
<action-mappings >
<action
name="stuForm"
path="/myAction"
input="index.jsp"
scope="request"
type="my.action.MyAction" /> </action-mappings>action里也没有任何setAttribute之类的代码,
为啥我在MyJsp.jsp里面用
<%out.print(((StuForm)request.getAttribute("stuForm")).getName()); %>
可以正常输出呢?
解决方案 »
- 给文章中的关键字自动添加链接
- apache服务器支持哪些脚本语言?
- 问个不该问的问题
- java 能做动态生静态的网站吗?能 怎么做
- url传繁体字怎么做?
- log4j问题 如何在程序中设置日志路径 急!!!!!!
- Hibernate统计时的java.lang.ClassCastException: java.lang.Long错误请解
- JSP寻求帮忙
- service方法中的doDelete,doGet,doPost,doPut,doTrace,doOptions请求有什么区别,它们何时将触发!
- 如何将jsp前端页面从java项目分离出来,做出单独的前端项目?
- jsp页面如何显示带有html标志的字符串
- 能否控制input file浏览限制
例如,假定Form Bean存在与request范围内,并且此项设为“myBean”,那么request.getAttribute("myBean")
就可以返回该Bean的实例。此项为可选项。
楼上的直接抄书上的吧
这个我也看到了,不过不是我关心的,我没用attribute照样可以getAttribute,那这个属性不是多余的吗
当你没有设置attribute属性的时候,request.getAttribute("myBean") 的myBean是你在<form-bean />里定义的name,当你设置attribute属性的时候是,应该用attribute的属性来调用request.getAttribute("myBean") 方法,这时候myBean是attribute的属性!
答案很简单,如果单从结果上看,此时struts使用的name的值,为什么呢,看struts源代码:/**
* The request-scope or session-scope attribute name under which our
* form bean is accessed, if it is different from the form bean's
* specified <code>name</code>.
*该代码在:org.apache.struts.config.ActionConfig中
*/
protected String attribute = null;public String getAttribute() {
//yes!!!!就在这里,看到了吧,如果你没有设定attribute,那么struts 会把name的值拿过来用。呵呵
if (this.attribute == null) {
return (this.name);
} else {
return (this.attribute);
}
}public void setAttribute(String attribute) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.attribute = attribute;
}