简体   繁体   English

从jsp页面将“空”值接收到Struts 2中

[英]Recieving 'null' values from a jsp page into Struts 2 Action

I have a jsp page and struts 2 Action class . 我有一个jsp页面和struts 2 Action类。 When I submit the form in the jsp , I am getting null values into the action. 当我在jsp中提交表单时,我将空值添加到操作中。

The JSP code looks like : JSP代码如下所示:

   <s:form id="user" name="user" action="initUserAdmin">

    <s:textfield name="userName"  cssClass="txtbox" size="30" />
    <div class="btn"><a href='<s:url action="searchUserAdmin"/>' 
    title="Search"    id="button" class="btn" ><span>Search</span></a></div>

   </s:form>

The struts.xml has this part struts.xml具有这一部分

      <action name="*UserAdmin" method="{1}" class="com.mphasis.im.web.action.UserAction">
        <result name="init" type="tiles">user</result>
        <result name="search" type="tiles">user</result>
        <result name="reset" type="tiles">user</result>
        <result name="createNew" type="tiles">createNewUser</result>
    </action>

And the Action class has this : 动作类具有以下内容:

        public class UserAction extends BaseAction
          {
            public String userName;

        public String getUserName()
        {
      return userName;
        }

        public void setUserName(String userName)
        {
      this.userName = userName;
        }

            public String search()
           {
            searchProcessed = true;

            System.out.println("******** inside search ******");

            System.out.println("username = "+ userName);

            return TilesConstants.SEARCH;
           }

And the output comes as below when I type a string in the text box in jsp page. 当我在jsp页面的文本框中键入一个字符串时,输出如下。

        ******** inside search ******
         username = null

What might be the problem ? 可能是什么问题? Am I missing something ? 我想念什么吗?

<a href='<s:url action="searchUserAdmin"/>' 
title="Search"    id="button" class="btn" >

It's just a href to the URL of action and with no parameter. 它只是操作URL的href,没有参数。 Therefore, the 'userName' in the action is null. 因此,操作中的“ userName”为空。

<s:form id="user" name="user" action="initUserAdmin">

    <s:textfield name="userName"  cssClass="txtbox" size="30" />
    <div class="btn"><a href='javascript:submitme()' 
    title="Search"    id="button" class="btn" ><span>Search</span></a></div>

   </s:form>

Javascript Java脚本

function submitme(){
document.user.submit()
}

Also in the action mapping you provided the action name you are using there is UserAdmin whereas in the form action you are using initUserAdmin .They must be the same 同样在操作映射中,您提供了您正在使用的操作名称,其中有UserAdmin而在表单操作中,您使用了initUserAdmin 。它们必须相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM