いつもお世話になっております。
早速本題に入らせていただきます。
やりたいこと
html側で動的に作成したリストをjava側で取得したいです。
環境
struts2、java、javascript、html使用して製作しています。
javaを用いているため、htmlはjspファイルに記述しています。
実際のコード
java
1private List<String> rList; 2 3public List<String> getrList() { 4 return rList; 5} 6 7public void setrList(List<String> rList) { 8 this.rList = rList; 9} 10 11//ボタンを押した時にNullチェック(仮処理) 12public String debug() throws Exception { 13 if((this.rList) == null){ 14 System.out.println("null"); 15 }else{ 16 System.out.println("Notnull"); 17 } 18 return "success"; 19} 20
html
1<s:form name="mit" > 2 <div id="tab1" class="tab"> 3 <h2>画面</h2> 4 <font color="red"><b><s:property value="errmsg" /></b></font> 5 <div class="ib-box" style="padding-right: 50px; padding-left: 10px;">あああ</div> 6 7 8 <!-- その他処理--> 9 10 <!-- ボタン --> 11 <div class="ib-box" > 12 <div class="buttonGroup"> 13 <s:submit method="debug" value="削除"/> 14 </div> 15 </div> 16 <!-- 送りたいリスト --> 17 <div class="ib-box" > 18 <form name="recipientList" action="" > 19 <input type="submit" value="送信" /> 20 <select id="rList" name="rList" multiple style="width: 140px;" size="10" ></select> 21 </form> 22 </div> 23 24 <!-- その他処理--> 25 </div> 26</s:form>
javascript
1 2//動的生成部分 3function OnceSelect(){ 4 var i 5 , eList = document.getElementById( 'eList' ) 6 , rList = document.getElementById( 'rList' ) 7 , eListoptions = eList.options 8 , el = eListoptions.length 9 , rListoptions = rList.options 10 , rl = rListoptions.length 11 , option; 12 13 for ( i = 0; i < el; i++ ) { 14 if ( eListoptions[ i ].selected === true ) { 15 for ( j = 0; j < rl; j++) { 16 if(eListoptions[ i ].text === rListoptions[ j ].text) return; 17 } 18 option = document.createElement( 'option' ); 19 option.innerHTML = eListoptions[ i ].text; 20 option.setAttribute( 'value', eListoptions[ i ].value ); 21 rList.appendChild( option ); 22 } 23 }
のような実装をしています。(フェイクのため誤字脱字があるかも知れません)
補足
今現在のstruts2のやり方をしていますが、もうどんな形でもいいのでjava側に値が返ればいいと思っています。
htmlは初心者のため、できれば詳しい説明をしていただけるとうれしいです。
どうかよろしくお願いします。
改修点
html
1 2<div id="tab1" class="tab"> 3 4 <h2>画面</h2> 5 <font color="red"><b><s:property value="errmsg" /></b></font> 6 <div class="ib-box" style="padding-right: 50px; padding-left: 10px;">あああ</div> 7 8 <!-- その他処理--> 9 10 <!-- ボタン郡 --> 11 <div class="ib-box"> 12 <div class="buttonGroup"> 13 <s:submit method="debug" value="削除" /> 14 </div> 15 </div> 16 17 <!-- 右側リスト --> 18 <div class="ib-box"> 19 <form name="recipientList" action=""> 20 <input type="submit" value="送信" /> 21 <select id="rList" name="rList" multiple style="width: 140px;" size="10"> 22 </select> 23 </form> 24 </div> 25 26 27 28 <s:form name="mit" onSubmit="return check()"> 29 <!-- その他処理--> 30 </s:form> 31</div>
上記のようにしましたが依然Nullが返ってきてしまいます。
F12キーで確認してみると、
html
1<select id="rList" name="rList" multiple style="width: 140px;" size="10"> 2 <option value="value値">田中太郎</option> 3</select>
のような形で生成されているので生成ミスではないと思うのですが。
ご教授のほうよろしくお願いします。
Actionクラス
java
1 2package action; 3 4import java.text.SimpleDateFormat; 5import java.util.ArrayList; 6import java.util.Calendar; 7import java.util.Date; 8import java.util.List; 9 10import org.apache.struts2.config.Result; 11import org.apache.struts2.config.Results; 12import org.apache.struts2.dispatcher.ServletRedirectResult; 13 14import bean.DivisionCodeBean; 15import bean.MonthBean; 16import bean.PushInfoBean; 17import bean.RecipientBean; 18 19import com.opensymphony.xwork2.ActionChainResult; 20 21import dao.DivisionCodeDAO; 22import dao.PushInsertDAO; 23import dao.RecipientDAO; 24 25 26@Results({ 27 @Result(name = "pv", value = "pV", type = ActionChainResult.class), 28 @Result(name = "psa", value = "pS", type=ServletRedirectResult.class) 29}) 30 31public class PSA extends AbstractAction { 32 private static final long serialVersionUID = 1L; 33 34 35 //メンバー郡 36 public String Id; 37 public String pass; 38 public String code; 39 public int s_code = 0; 40 public String year; 41 public String month; 42 public String date; 43 public String errmsg; 44 public String hon; 45 46 //今日の日付取得 47 Calendar cal = Calendar.getInstance(); 48 Date now = new Date(); 49 SimpleDateFormat sdf1 = new SimpleDateFormat("YYYY/MM"); 50 String nowdate= sdf1.format(now); 51 52 public String Name; 53 54 55 public String execute() throws Exception { 56 57 if((this.rList) == null){ 58 System.out.println("null"); 59 }else{ 60 System.out.println("Notnull"); 61 } 62 63 this.LoginName = (String)this.sessionMap.get("Name"); 64 this.userId = (String)this.sessionMap.get("Id"); 65 66 DDAO dcDAO = new DDAO(); 67 dcBean = dcDAO.getDC(); 68 pC = dcBean.getDList(); 69 70 if(this.division_code == null){ 71 RDAO rDAO = new RDAO(); 72 rBean = rDAO.getData_R(); 73 pR = rBean.getRDate_List(); 74 }else{ 75 RDAO rDAO = new RDAO(); 76 s_d_code = Integer.parseInt(this.d_code); 77 if(s_d_code == 0){ 78 rBean = rDAO.getData_R(); 79 80 }else{ 81 rBean = rDAO.getData_R(s_d_code); 82 } 83 pR = rBean.getRtDate_List(); 84 } 85 return "success"; 86 } 87 88 public String search() throws Exception { 89 return "pv"; 90 } 91 92 public String debug() throws Exception { 93 if((this.rList) == null){ 94 System.out.println("null"); 95 }else{ 96 System.out.println("Notnull"); 97 } 98 return "success"; 99 } 100 101 102 @SuppressWarnings("unchecked") 103 public String s() throws Exception { 104 105 RDAO rDAO = new RDAO(); 106 s_d_code = Integer.parseInt(this.d_code); 107 108 if(s_d_code == 0){ 109 rBean = rDAO.getData_R(); 110 }else{ 111 rBean = rDAO.getData_R(s_n_code); 112 } 113 pR = rBean.getRDate_List(); 114 115 PIDAO pDAO = new PIDAO(); 116 pDAO.iP(103, this.hon, pR); 117 118 119 this.errmsg = "qqqqqq"; 120 return "psa"; 121 } 122 123 public DBean getModeler1() {return dcBean;} 124 public ArrayList<DCBean> getLister1(){return pC;} 125 public void setLister1(ArrayList<DCBean> lister1) {this.pC = lister1;} 126 127 public RBean getModeler() {return rBean;} 128 public ArrayList<RBean> getLister2(){return pR;} 129 public void setLister2(ArrayList<RBean> lister2) {this.pR = lister2;} 130 131 //リストグループ取得用 132 private List<String> rList; 133 public List<String> getrList() {return rList;} 134 public void setrList(List<String> rList) {this.rList = rList;}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/07/11 02:48
2016/07/11 09:00
2016/07/12 00:13
2016/07/19 00:14