简单Struts问题?

yxqc 2005-10-27 03:57:10
以下两个name该怎么理解,或着说一样不一样的?
<logic:present name="personbean" scope="request">
<bean:write name="personbean" property="userName" />!
</logic:present>
...全文
273 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
yxqc 2005-10-28
  • 打赏
  • 举报
回复
那为什么我如果不填内容,提交上去会出错,不会执行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;
yangkun110 2005-10-28
  • 打赏
  • 举报
回复
现在可以了,我觉得是这个原因。在request.setAttribute("key",name) //这个地方的name应该是一个FormObject, 不应该是单个文本框, 后面的才有效
<logic:present name="key" scope="request">
<h2>
<bean:write name="key" property="userName"/>!<p>
<bean:write name="key" property="passWord"/>!<p>
</h2>
</logic:present>
ianok 2005-10-28
  • 打赏
  • 举报
回复
up
yxqc 2005-10-28
  • 打赏
  • 举报
回复
大家帮忙一下哪。
classjava 2005-10-27
  • 打赏
  • 举报
回复
^_^
yxqc 2005-10-27
  • 打赏
  • 举报
回复
现在可以了,我觉得是这个原因。在request.setAttribute("key",name)
这里也要改成。
<logic:present name="key" scope="request">
<h2>
<bean:write name="key" property="userName"/>!<p>
<bean:write name="key" property="passWord"/>!<p>
</h2>
</logic:present>

是不是这样的,反正我是这样调试出来的。
classjava 2005-10-27
  • 打赏
  • 举报
回复
页面那个PersonBean改成小写
classjava 2005-10-27
  • 打赏
  • 举报
回复
^_^,你挺有耐心啊,我也是^_^
classjava 2005-10-27
  • 打赏
  • 举报
回复
request.setAttribute("personbean",pb.getUserName());
改成
request.setAttribute("personbean",pb);
request.setAttribute("userName",pb.getUserName());
yxqc 2005-10-27
  • 打赏
  • 举报
回复
我看了,但还是不太理解?能否解释一下, classjava(原始野人)
classjava 2005-10-27
  • 打赏
  • 举报
回复
建议:看一下struts原理
yxqc 2005-10-27
  • 打赏
  • 举报
回复
难道这里的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;


yxqc 2005-10-27
  • 打赏
  • 举报
回复
根本不能这个问题,我改了一样的。
照理说,request.setAttribute("personbean",pb.getUserName());
没有错误把pb.getUserName()放入名字为personbean里。
classjava 2005-10-27
  • 打赏
  • 举报
回复
<bean:write name="PersonBean" property="userName"/>
这是个对象,personBean里面有这个userName的属性,你那个只是一个字符对象而已
^_^,结贴啊,你在广州吗
yxqc 2005-10-27
  • 打赏
  • 举报
回复
request.setAttribute("personbean",pb.getUserName());
改成
request.setAttribute("personbean",pb);

为什么,难道不一样吗?
classjava 2005-10-27
  • 打赏
  • 举报
回复
request.setAttribute("personbean",pb.getUserName());
改成
request.setAttribute("personbean",pb);
yxqc 2005-10-27
  • 打赏
  • 举报
回复
package helloapp;

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 {

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"));

}
}
yxqc 2005-10-27
  • 打赏
  • 举报
回复
package helloapp;

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;



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;
}
}
yxqc 2005-10-27
  • 打赏
  • 举报
回复
看看我下面这个为什么出不来呢?


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>
classjava 2005-10-27
  • 打赏
  • 举报
回复
不过多了一层保证,本来他会在各个生命周期里面找这个name为personbean的对象
加载更多回复(3)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧