jsp如下:
<s:form theme="simple" validate="false">
<s:fielderror><s:param>用户名</s:param></s:fielderror>
<table cellpadding="0" cellspacing="0" width="50%" align="center" class="table_login">
<tr>
<td width="2%" class="td_star" colspan="1">*</td>
<td width="15%" align="center" colspan="1">用户名:</td>
<td width="88%" colspan="2"><s:textfield key="userId" maxlength="10"></s:textfield></td>
</tr>
<tr>
<td width="2%" class="td_star" colspan="1">*</td>
<td width="15%" align="center" colspan="1">密&nbsp;&nbsp;&nbsp;&nbsp;码:</td>
<td width="88%" colspan="2"><s:password key="key" maxlength="10"></s:password></td>
</tr>
<tr>
<td align="center" colspan="4" height="10px"></td>
</tr>
<tr>
<td align="right" width="17%" colspan="2"></td>
<td align="center" width="15%" colspan="1"><s:submit action="Login_toMenu" key="登录" cssClass="button"/></td>
<td align="left" width="68%" colspan="1"><s:submit action="Login_toRegister" key="注册" cssClass="button"/></td>
</tr>
</table>
</s:form>配置如下:
    <package name="account-support" namespace="/common" extends="account-default">
        <action name="Login_*" method="{1}" class="account.pan.action.Login">
            <result name="login">/common/Menu.jsp</result>
            <result name="input">/common/Register.jsp</result>
            <result name="error">/common/Login.jsp</result>
            <result name="success">/common/Login.jsp</result>
            <interceptor-ref name="validation"/>
        </action>
    </package>
action如下:
public String toMenu()
{
System.out.println("userid:" + this.getUserId());
if (this.getUserId() == null || "".equals(this.getUserId()))
{
this.addFieldError("用户名", "请输入用户名");
return ERROR;
}
return LOGIN;
}
问题,不管用户名是否输入值了,为什么每次按下登录,userid总是为空?

解决方案 »

  1.   

    把 <s:textfield key="userId" maxlength="10"> key改成name试试看吧
      

  2.   

    <s:form theme="simple" validate="false"> 
    <FORM NAME="myForm" ACTION="Daoru"  METHOD="post">应该有个action 连接吧
      

  3.   

    <html:form action="/friend.do?"  focus="keyword" onsubmit="return queryPrepared(this)">

    <html:submit value="查询" />

    <html:reset value="重置" />
              <html:hidden property="queryCondition"/>
    </html:form>
      

  4.   

    楼上的,我的action已经正常触发了,所以我想数据有没有提交是不是和form有没有设置action没有关系
      

  5.   

    private String userId; /**
     * @return the userId
     */
    public String getUserId() {
    return userId;
    }
    /**
     * @param userId the userId to set
     */
    public void setUserId(String userId) {
    this.userId = userId;
    }这里的set方法始终未被调用,为什么呢
      

  6.   

     <s:submit action="Login_toMenu" key="登录" cssClass="button"/> </td> 
     <s:submit action="Login_toRegister" key="注册" cssClass="button"/> </td> 
    改成:
     <s:submit action="Login_toMenu.action" key="登录" cssClass="button"/> </td> 
     <s:submit action="Login_toRegister.action" key="注册" cssClass="button"/> </td> 
      

  7.   

    问题找到:      <package name="account-support" namespace="/common" extends="account-default"> 
            <action name="Login_*" method="{1}" class="account.pan.action.Login"> 
                <result name="login">/common/Menu.jsp </result> 
                <result name="input">/common/Register.jsp </result> 
                <result name="error">/common/Login.jsp </result> 
                <result name="success">/common/Login.jsp </result> 
                <interceptor-ref name="validation"/>   // 去掉
            </action> 
        </package> 把validation拦截器去掉即可。建议:
    <s:textfield key="userId" maxlength="10"> </s:textfield> 把key换成name。
    <s:submit action="Login_toMenu" key="登录" cssClass="button"/> 把key换成value。提示以下警告信息,但不影响运行2009-05-10 WARN [org.apache.struts2.util.TextProviderHelper] - [The first TextProvider in the ValueStack (com.opensymphony.xwork2.ActionSupport) could not locate the message resource with key 'userId'} - [com.opensymphony.xwork2.util.logging.commons.CommonsLogger.warn(CommonsLogger.java:45)}
    2009-05-10 WARN [org.apache.struts2.util.TextProviderHelper] - [The default value expression 'userId' was evaluated and did not match a property.  The literal value 'userId' will be used.} - [com.opensymphony.xwork2.util.logging.commons.CommonsLogger.warn(CommonsLogger.java:45)}