[Ajax4JSF] JSF页面传参问题,hurry!!!
xkqtest
2011-03-25
小弟有个问题请教一下,首先看代码及配置:
1。一个简单的Bean package com.test; public class TestBean { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 2.Action: package com.test.action; import com.test.TestBean; public class TestAction { private TestBean testBean; private String iputPara; public String process(){ testBean=reviObject(); System.out.println("in proccess username: "+testBean.getName()); System.out.println("in proccess age: "+testBean.getAge()); System.out.println("in proccess submit para: "+iputPara); return "test_result"; } private TestBean reviObject(){ TestBean te=new TestBean(); te.setName("test"); te.setAge(18); return te; } public TestBean getTestBean() { if(null!=testBean){ System.out.println("in getTestBean username:"+testBean.getName()); System.out.println("in getTestBean age:"+testBean.getAge()); }else{ System.out.println("in getTestBean is null" ); } return testBean; } public void setTestBean(TestBean testBean) { this.testBean = testBean; } public String getIputPara() { System.out.println("in getIputPara:"+iputPara); return iputPara; } public void setIputPara(String iputPara) { this.iputPara = iputPara; } } 3.input page: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <html> <f:view> <head></head> <body> <h:form id="tst_form"> <h:inputText id="tst_input" value="#{testAction.iputPara }" /> <h:commandButton id="test" action="#{testAction.process }" value="submit"/> </h:form> </body> </f:view> </html> 4.result page: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <html> <f:view> <head> </head> <body> name: <h:outputLabel value="#{testAction.testBean.name }"/><br/> age:<h:outputLabel value="#{testAction.testBean.age }"/><br/> action:<h:outputLabel value="#{testAction.iputPara }"/><br/> </body> </f:view> </html> 5.config: <managed-bean> <managed-bean-name>testAction</managed-bean-name> <managed-bean-class>com.test.action.TestAction</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>testBean</property-name> <value>#{testBean}</value> </managed-property> </managed-bean> <managed-bean> <managed-bean-name>testBean</managed-bean-name> <managed-bean-class>com.test.TestBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> <navigation-rule> <navigation-case> <from-outcome>test_result</from-outcome> <to-view-id>/test/test_result.jsp</to-view-id> <redirect/> </navigation-case> </navigation-rule> 现在问题是,我在input页面输入 “abcd” 后提交。在控制台打印出以下内容: [11-3-25 21:02:36:125 CST] 0000001c SystemOut O in getIputPara:null [11-3-25 21:02:36:125 CST] 0000001c SystemOut O in proccess username: test [11-3-25 21:02:36:125 CST] 0000001c SystemOut O in proccess age: 18 [11-3-25 21:02:36:125 CST] 0000001c SystemOut O in proccess submit para: abcd [11-3-25 21:02:36:265 CST] 0000001c SystemOut O in getTestBean username:null [11-3-25 21:02:36:265 CST] 0000001c SystemOut O in getTestBean age:0 [11-3-25 21:02:36:265 CST] 0000001c SystemOut O in getTestBean username:null [11-3-25 21:02:36:265 CST] 0000001c SystemOut O in getTestBean age:0 [11-3-25 21:02:36:265 CST] 0000001c SystemOut O in getIputPara:null 而在result 页面username,IputPara 和age都没有值。请高手说一下,这是什么原因,在process中明明有值去调用get的时候就没有了。 如果我想在结果页面将这三个值显示出来该如何修改。另外为什么去结果页面时get方法调用了两次。 由于我刚开始接触JSF,请大家多多指教,谢谢。 |
|
xkqtest
2011-03-26
怎么没有人帮我呢?
|
|
yyq2008aa
2011-03-26
testBean没必要配置进来。managedbean相互引用时好像有生命周期长短的问题。
|
|
yancheng278
2011-03-30
1.因为testAction的范围为request,第二次打开时,为一个新的testAction。
2.在结果页面两次调用了testBean |
|
li2005
2011-03-31
我看了半天才看出问题,楼上正解,厉害
|
|
微雨心晴
2011-04-01
还在使用JSF 1.1/1.2?
|