<logic:present name="personbean" scope="request">
是在判断personbean 是否存在,存在的话就执行
<bean:write name="personbean" property="userName" />
来输出userName
是在判断personbean 是否存在,存在的话就执行
<bean:write name="personbean" property="userName" />
来输出userName
解决方案 »
- 【Spring】实现ApplicationListener接口,为什么会多生成一个实例?
- 关于struts2生成验证码问题
- filter能否打成war包,如何部署?
- 如果何使用sun.net.ftp.FtpClient操作文件夹,
- struts2上传文件demo的问题
- java连接oracle数据库时报错
- 请大家帮忙给点复习java基础,3大框架的复习资料。
- 关于session的问题
- 购买:weblogic_sp.jar文件...
- 请介绍一些J2EE的好书(包括EJB,JSP),千万不要是E文版的书,
- IBatis的错误 紧急求救
- 从一个Size为10000的List里面把数据复制到一个Length为10000的Object数组并强制转换竟然毫无费时!!!
<bean:write name="personbean" property="userName" />!
</logic:present>
一样的
hello.jsp
<%@ 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" %><html:html locale="true">
<head>
<title><bean:message key="hello.jsp.title"/></title>
<html:base/>
</head>
<body bgcolor="white"><p> <h2><font color="#FF0000">
<bean:message key="hello.jsp.body" /></font><br><hr>
<bean:message key="hello.jsp.page.heading"/></h2><p> <html:errors/><p> <logic:present name="PersonBean" scope="request">
<h2>
<bean:write name="PersonBean" property="userName"/>!<p>
<bean:write name="PersonBean" property="passWord"/>!<p>
</h2>
</logic:present>
<html:form action="/HelloWorld.do" focus="userName" > <bean:message key="hello.jsp.prompt.person"/><br>
<html:text property="userName" size="16" maxlength="16"/><br>
<bean:message key="hello.jsp.prompt.password"/><br>
<html:password property="passWord" size="16" maxlength="16"/><br>
<html:submit property="submit" value="Submit"/>
<html:reset/> </html:form><br> </body>
</html:html>
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; public String getUserName() {
return (this.userName);
} public void setUserName(String userName) {
this.userName = userName;
} public void reset(ActionMapping mapping, HttpServletRequest request) {
this.userName = null;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ((userName == null) || (userName.length() < 1))
errors.add("username", new ActionMessage("hello.no.username.error")); if ((passWord == null) || (passWord.length() < 1))
errors.add("password", new ActionMessage("hello.no.username.error"));
return errors;
}
public String getPassWord()
{
return passWord;
}
public void setPassWord(String passWord)
{
this.passWord = passWord;
}
}
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 { 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();
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()));
}
PersonBean pb = new PersonBean();
pb.setUserName(userName);
pb.setPassWord(passWord);
request.setAttribute("personbean",pb.getUserName());
request.setAttribute("password",pb.getPassWord());
// 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")); }
}
改成
request.setAttribute("personbean",pb);
免费招聘、求职。IT人工作的天堂。希望能够为大家所用。
改成
request.setAttribute("personbean",pb);为什么,难道不一样吗?
这是个对象,personBean里面有这个userName的属性,你那个只是一个字符对象而已
^_^,结贴啊,你在广州吗
照理说,request.setAttribute("personbean",pb.getUserName());
没有错误把pb.getUserName()放入名字为personbean里。
request.setAttribute("PersonBean",pb);
request.setAttribute("PersonBean",pb);
要跟这里的personbean对住 <logic:present name="PersonBean" scope="request">
<h2>
<bean:write name="PersonBean" property="userName"/>!<p>
<bean:write name="PersonBean" property="passWord"/>!<p>
</h2>
</logic:present>那为什么我如果不填内容,提交上去会出错,不会执行ActionForm里的 if ((userName == null) || (userName.length() < 1))
errors.add("username", new ActionMessage("hello.no.username.error")); if ((passWord == null) || (passWord.length() < 1))
errors.add("password", new ActionMessage("hello.no.username.error"));
return errors;
改成
request.setAttribute("personbean",pb);
request.setAttribute("userName",pb.getUserName());
这里也要改成。
<logic:present name="key" scope="request">
<h2>
<bean:write name="key" property="userName"/>!<p>
<bean:write name="key" property="passWord"/>!<p>
</h2>
</logic:present>是不是这样的,反正我是这样调试出来的。
errors.add("username", new ActionMessage("hello.no.username.error")); if ((passWord == null) || (passWord.length() < 1))
errors.add("password", new ActionMessage("hello.no.username.error"));
return errors;
<logic:present name="key" scope="request">
<h2>
<bean:write name="key" property="userName"/>!<p>
<bean:write name="key" property="passWord"/>!<p>
</h2>
</logic:present>