<logic:present name="personbean" scope="request">
是在判断personbean 是否存在,存在的话就执行
<bean:write name="personbean" property="userName" />
来输出userName

解决方案 »

  1.   

    <logic:present name="personbean" scope="request">
             <bean:write name="personbean" property="userName" />!
        </logic:present>
    一样的
      

  2.   

    那应该两个name都是属于模型组件PersonBean了,是一样的,以吧。
      

  3.   

    不过多了一层保证,本来他会在各个生命周期里面找这个name为personbean的对象
      

  4.   

    看看我下面这个为什么出不来呢?
    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>
      

  5.   

    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;
      }
    }
      

  6.   

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

  7.   

    request.setAttribute("personbean",pb.getUserName());
    改成
     request.setAttribute("personbean",pb);
      

  8.   

    我顶!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!
    免费招聘、求职。IT人工作的天堂。希望能够为大家所用。
      

  9.   

    request.setAttribute("personbean",pb.getUserName());
    改成
     request.setAttribute("personbean",pb);为什么,难道不一样吗?
      

  10.   

    <bean:write name="PersonBean" property="userName"/>
    这是个对象,personBean里面有这个userName的属性,你那个只是一个字符对象而已
    ^_^,结贴啊,你在广州吗
      

  11.   

    根本不能这个问题,我改了一样的。
    照理说,request.setAttribute("personbean",pb.getUserName());
    没有错误把pb.getUserName()放入名字为personbean里。
      

  12.   

    难道这里的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;
      

  13.   

    我看了,但还是不太理解?能否解释一下, classjava(原始野人)
      

  14.   

    request.setAttribute("personbean",pb.getUserName());
    改成
     request.setAttribute("personbean",pb);
     request.setAttribute("userName",pb.getUserName());
      

  15.   

    现在可以了,我觉得是这个原因。在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>是不是这样的,反正我是这样调试出来的。
      

  16.   

    那为什么我如果不填内容,提交上去会出错,不会执行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;
      

  17.   

    现在可以了,我觉得是这个原因。在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>