java 提交表单

问题是这样的
一个表单,表单里含有多个DIV
每个DIV 都是相同的内容 姓名 年龄 性别 出生地 等等!

我怎么判断,哪个DIV的有值 ,哪个div没值 ,最后如何只提交有值的div内容给action

我的是struts1!! 求高手指点!!

实现代码如下:
public class Demo {

public static void main(String[] args) throws Exception {
Map m = new HashMap();
String url = "http://www.ecice06.com/cn/search_gjdo.asp";
String code = "GB2312";

// m.put("sel_zazhimc", "");
//
// m.put("sel_niandu", "");
//
// m.put("txt_qishiye", "");
//
// m.put("txt_doi", "");
// m.put("xueke", "");
// m.put("zhuanye", "");
// m.put("txt_zuozhe", "");
// m.put("txt_zuozhe2", "");
// m.put("txt_zuozhedw", "");
//
// m.put("txt_zhaiyao", "");
// m.put("txt_guanjianci", "");
// m.put("txt_fenleihao", "");
// m.put("sel_niandus", "");
// m.put("sel_niandue", "");

m.put("txt_wenti", "数据");
m.put("pagesize", "10");
m.put("Submit2", "查询");
m.put("rad_px", "zuozhexm,kanchurq desc");
String rus = doPost(url, m, code);

System.out.println(rus);
}

public static String doPost(String reqUrl, Map parameters, String recvEncoding) {
HttpURLConnection conn = null;
String responseContent = null;
try {
StringBuffer params = new StringBuffer();
for (Iterator iter = parameters.entrySet().iterator(); iter.hasNext();) {
Entry element = (Entry) iter.next();
params.append(element.getKey().toString());
params.append("=");
params.append(URLEncoder.encode(element.getValue().toString(), recvEncoding));
params.append("&");
}

if (params.length() > 0) {
params = params.deleteCharAt(params.length() - 1);
}
URL url = new URL(reqUrl);
HttpURLConnection url_con = (HttpURLConnection) url.openConnection();
url_con.setRequestMethod("POST");
// System.setProperty("sun.net.client.defaultConnectTimeout", String
// .valueOf(HttpRequestProxy.connectTimeOut));// (单位:毫秒)jdk1.4换成这个,连接超时
// System.setProperty("sun.net.client.defaultReadTimeout", String
// .valueOf(HttpRequestProxy.readTimeOut)); // (单位:毫秒)jdk1.4换成这个,读操作超时
url_con.setConnectTimeout(5000);//(单位:毫秒)jdk
// 1.5换成这个,连接超时
url_con.setReadTimeout(5000);//(单位:毫秒)jdk 1.5换成这个,读操作超时
url_con.setDoOutput(true);
byte[] b = params.toString().getBytes();
url_con.getOutputStream().write(b, 0, b.length);
url_con.getOutputStream().flush();
url_con.getOutputStream().close();

InputStream in = url_con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in, recvEncoding));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
String crlf = System.getProperty("line.separator");
while (tempLine != null) {
tempStr.append(tempLine);
tempStr.append(crlf);
tempLine = rd.readLine();
}
responseContent = tempStr.toString();
rd.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return responseContent;
}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-12-13
如果你用的是struts标签,直接用配置里对应的表名.属性就行啦!
代码如下:=======struts-config.xml文件=============
<form-beans >
<form-bean name="FindByProductForm" type="com.ynb.dao.product.Product" />
</form-beans>
<action-mappings>
<action path="/FindByProduct"
type="com.ynb.dao.actions.FindByProductAction"
input="/index.jsp"
name="FindByProductForm"
parameter="FindByProduct"
validate="true">
<forward name="ok" path="/FindByProductList.jsp" />
<forward name="no" path="/error.jsp" />
</action>
</action-mappings>

JSP页面===================

<html:form method="post" action="/FindByProduct">
这里加上你的内容
<input type="button" value="查 询" style="width: 100; height: 30" onclick="javascript:check();"/>
</html:form>
<script type="text/javascript">
function check(){
document.FindByProductForm.字段的属性.value=“”;这里就可以判断值啦
}
</script>
第2个回答  2011-12-13
一般来说这种好像是列表展示页面,那应该有ID/主键值,只把这个值传到action就ok了追问

能具体说说嘛 因为上面那个是举个例子说明 公司一共做5个相同的DIV,而每个div都有10个以上的文本框或者下拉框,这样5个加起来最起码就50个以上了,如果按照传统的,JS每个都用不同的变量来获取值的话,那代码量会很大,我感觉很麻烦,有没简便点的!

追答

不太清楚你的需求,如果非得用js做的话就用jquery吧,操作简单方便,代码量也不大

追问

举个例子吧

不一定都要填!也不一定从第1个开始填,我第一个可以不填,从第2个开始填!然后在结合我开始的问题

追答

var alltext=$("form > div > input[type='text']");获取到form里所有的text控件
var div;
$(alltext).each(function(i){
//如果text框有值获取父级div元素
//
if($(this).val()!=''){
div+=$(this).parent();
}
});
你可以在页面上放一个隐藏的空form,将上面找到的div内容用
$("#formid").append(div);
添加到form里提交

本回答被提问者采纳
第3个回答  2011-12-13
每个div都有它的id的。根据id判断。追问

能具体说说嘛 因为上面那个是举个例子说明 公司一共做5个相同的DIV,而每个div都有10个以上的文本框或者下拉框,这样5个加起来最起码就50个以上了,如果按照传统的,JS每个都用不同的变量来获取值的话,那代码量会很大,我感觉很麻烦,有没简便点的!

追答


function tt(){
var aa = document.getElementById("div1");
var hh = document.getElementById("div2");
if(aa.innerHTML==""){
aa.style.display="none"
}
if(aa.innerHTML !==""){
hh.style.display="none"
}

这是网上找的一个简单的例子。如果div1为空就显示div2