在精通struts的例子里加了个文本框password,但显示的还是username内容,而不是password提交的内容,怎么回师?????
hello.jsp
<%@ page contentType= "text/html;charset=gb2312 " language= "java " %>
<%@ taglib uri= "/WEB-INF/struts-bean.tld " prefix= "bean " %>
<%@ taglib uri= "/WEB-INF/struts-html.tld " prefix= "html " %>
<%@ taglib uri= "/WEB-INF/struts-logic.tld " prefix= "logic " %>
<!-- UTF-8-->
<html:html locale= "true ">
<head>
<title> <bean:message key= "hello.jsp.title "/> </title>
<html:base/>
</head>
<body bgcolor= "white "> <p>
<h2> <bean:message key= "hello.jsp.page.heading "/> </h2> <p>
<html:errors/> <p>
<logic:present name= "personbean " scope= "request ">
<h2>
<bean:message key= "hello.jsp.page.name "/>
<bean:write name= "personbean " property= "userName " /> ! <br> <!--personbean.java的getUserName()=hello weiqin -->
<bean:message key= "hello.jsp.page.ps "/>
<bean:write name= "personbean " property= "passWord " />
</h2>
</logic:present>
<html:form action= "/HelloWorld.do " > <!--focus= "userName " -->
<bean:message key= "hello.jsp.prompt.person "/>
<html:text property= "userName " size= "16 " maxlength= "16 "/> <!-- --> <br>
<bean:message key= "hello.jsp.prompt.ps "/>
<html:text property= "passWord " size= "16 " maxlength= "16 "/> <br>
<html:submit property= "submit " value= "Submit "/>
<html:reset/>
</html:form> <br>
<html:img page= "/struts-power.gif " alt= "Powered by Struts "/>
</body>
</html:html>
helloaction.java
package hello;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
public final class HelloAction extends Action {
/**
* Process the specified HTTP request, and create the corresponding HTTP
* response (or forward to another web component that will create it).
* Return an <code> ActionForward </code> instance describing where and how
* control should be forwarded, or <code> null </code> if the response has
* already been completed.
*/
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// These "messages " come from the ApplicationResources.properties file
MessageResources messages = getResources(request);
/*
* Validate the request parameters specified by the user
* Note: Basic field validation done in HelloForm.java
* Business logic validation done in HelloAction.java
*/
ActionMessages errors = new ActionMessages();
String userName = (String)((HelloForm) form).getUserName();
String passWord = (String)((HelloForm) form).getPassWord();///////////// "mima "从helloform得到
String badUserName = "Monster ";
if (userName.equalsIgnoreCase(badUserName)) {
errors.add( "username ", new ActionMessage( "hello.dont.talk.to.monster ", badUserName ));
//saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
/*
* Having received and validated the data submitted
* from the View, we now update the model
*/
PersonBean pb = new PersonBean();
pb.setUserName(userName);
pb.setPassWord(passWord);/////////////////////////String passWord = "mima ";
pb.saveToPersistentStore();
/*
* If there was a choice of View components that depended on the model
* (or some other) status, we 'd make the decision here as to which
* to display. In this case, there is only one View component.
*
* We pass data to the View components by setting them as attributes
* in the page, request, session or servlet context. In this case, the
* most appropriate scoping is the "request " context since the data
* will not be neaded after the View is generated.
*
* Constants.PERSON_KEY provides a key accessible by both the
* Controller component (i.e. this class) and the View component
* (i.e. the jsp file we forward to).
*/
request.setAttribute( Constants.PERSON_KEY, pb);
// Remove the Form Bean - don 't need to carry values forward
request.removeAttribute(mapping.getAttribute());
// Forward control to the specified success URI
return (mapping.findForward( "SayHello "));
}
}
helloform.java
package hello;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public final class HelloForm extends ActionForm {
private String userName = null;
private String passWord = null;//////haha
public String getUserName() {
return (this.userName);
}
public void setUserName(String userName) {
this.userName = userName;
}
///////////////////
public String getPassWord() {
return (this.userName);
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
///////////////////////
/**
* Reset all properties to their default values.
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.userName = null;
}
/**
* Validate the properties posted in this request. If validation errors are
* found, return an <code> ActionErrors </code> object containing the errors.
* If no validation errors occur, return <code> null </code> or an empty
* <code> ActionErrors </code> object.
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (( passWord== null) || (passWord.length() < 1))
errors.add( "username ", new ActionMessage( "hello.no.username.error "));//////userName userName
return errors;
}
}
personbean.java
package hello;
public class PersonBean {
private String userName = null;
private String passWord = null;////////
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
///////////////////////////
public String getPassWord() {
return this.passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
//////////////////////////
/**
* This is a stub method that would be used for the Model to save
* the information submitted to a persistent store. In this sample
* application it is not used.
*/
public void saveToPersistentStore() {
/*
* This is a stub method that might be used to save the person 's
* name to a persistent store(i.e. database) if this were a real application.
*
* The actual business operations that would exist within a Model
* component would depend upon the requirements of the application.
*/
}
}
哪里不对啊?两个都始终显示username的内容
[解决办法]
public String getPassWord() {
return (this.userName);
}
你这里写错了,仔细检查一下代码,呵呵
actionForm里面getPassWord返回的还是userName
[解决办法]
gaoyunpeng(月光鸟)--》正解