2

我正在使用 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;
}
    ......
    ......
    }

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

4

1 回答 1

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. 
}
于 2013-03-15T17:28:55.060 回答