如何在struts中的action类中获取更改的单选按钮值

时间:2013-03-14 23:01:45

标签: jsp struts-1

我正在使用struts 1.3开发应用程序。 在我的JSP页面中,表格将所有注册的用户详细信息显示为每行一条记录。

每行包含两个单选按钮,FM和“ODP”。在用户注册期间,默认值将为所有用户的FM。

管理员必须选择特定用户并将其选项更改为ODP。管理员可以更改所有用户的选项,也可以跳过部分用户。按下提交按钮后,如何根据管理员的选择逐个读取所有更改的单选按钮值以更新数据库。

每当我在更改值后按下提交按钮,我在动作类中都会获得相同的旧值。如何访问所选单选按钮的新值?

对此的任何帮助都将受到高度赞赏。

这是我的示例代码 -

JSP -

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>


<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>JSP Page</title>

</head>

<body>
    <div style="color:red">
   <!--     <html:errors/> --> 
    </div>
    <html:form  action="/User.dp?parameter=save" >
    <div>
<div style="float:left;padding-left:30px;">UserName</div> 
<div style="float:left;padding-left:30px;">Address</div>
<div style="float:left;padding-left:30px;">Satus</div>
<div style="float:left;padding-left:30px;">Radio</div>
<div style="clear:both;"></div>
</div>
<logic:iterate  id="listUserIdDetails" name="UserForm" property="listUserId">
<p>
<div>
<div style="float:left;padding-left:30px;"><bean:write name="listUserIdDetails" property="name"/> </div> 
<div style="float:left;padding-left:60px;"><bean:write name="listUserIdDetails" property="address"/></div>
<div style="float:left;padding-left:60px;"><bean:write name="listUserIdDetails" property="active"/></div>
 <div style="float:left;padding-left:60px;"><html:radio indexed="true" property="active" name="listUserIdDetails" value="001"   /> <html:radio indexed="true"  property="active" name="listUserIdDetails"  value="002" /></div>
  </div><div style="clear:both;"></div>
  </logic:iterate>
  <html:submit value="submit"  ></html:submit>
       </html:form>
  </body>
  </html>

这是我的动作类 -

  public ActionForward save(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    UserListForm userForm = (UserListForm) form;
    ArrayList alist=userForm.getListUserId();

     for(int i=1;i<alist.size();i++)
     {
         UserListForm ulist= (UserListForm)alist.get(i);
        System.out.println("User:"+ulist.getName()+":"+ulist.getActive()); 
     }
    System.out.println(userForm.getName()+":"+userForm.getActive());

        return mapping.findForward("success");

  }
 }

表 -

    public class UserListForm extends ActionForm {

private String name=null;
private String address=null;
private String active=null;
private ArrayList<UserListForm> listUserId=null;
public ArrayList<UserListForm> getListUserId() {
    return listUserId;
}
public void setListUserId(ArrayList<UserListForm> listUserId) {
    this.listUserId = listUserId;
}
    ......
    ......
    }

如果管理员更改了单选按钮,请帮助我在操作类中获取更改的值。

1 个答案:

答案 0 :(得分:3)

我认为你应该沿着这条路做点什么。

<html:radio name="userListForm " property="myRadio" value="FM" >FM</html:radio>
<html:radio name="userListForm " property="myRadio" value="ODP" >ODP</html:radio>

public class UserListForm { 
    private String myRadio;
    public String getMyRadio() {
        return myRadio;     
    }
    public void setMyRadio(String myRadio) {
        this.myRadio = myRadio;
    }
}

public class MyAction{
     public ActionForward save(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

        UserListForm userForm = (UserListForm) form;
        String radioSelected = userForm.getMyRadio();//This will return the value of the "myRadio" in this case it will be either "FM" or "ODP" depending on which one you selected. 
}

String radioSelected = userForm.getMyRadio() ---此语句将返回所选单选按钮的值。因此,您可以检查此返回的值,并根据需要执行。 例如。

if(radioSelected.equals("FM"){
     //do something
}else if(radioSelected.equals("ODB"){
     //do something else. 
}