在做java web开发中,经常性使用的是request.getParameter(""), 方法,来获取从表单传递的内容,如果,字段数量不是很多的话,可以不考虑,如果字段特别多的时候,
复制粘贴很烦?
方法1 使用 request.getParameterMap(),遍历。 具体怎么使用,请度娘。
方法2 使用Python,写文件形式,并且,此形式扩展很容易。
1、安装Python环境。具体怎么安装,请网上搜索,一大堆。
2、将Python安装路径,设置到Path路径上。(主要是windows环境,linux 自带Python,不需要自己安装)
环境安装好之后,新建需要获取的参数文件,如:
abc1
abc2
abc3
abc4
准备工作到此结束,新建一个buildFile.py文件,内容如下:
#coding=utf-8;
#功能:根据指定的文件参数,生成指定格式的变量,
#例如:根据str1 生成 str1=request.getParameter("str1");
#或者,将文件内容添加jsonObject.put("str1",str1);这种形式
import sys;
def readFile(rFile):
""" 根据指定文件路径,读取文件内容,并返回dict"""
contDict={};#定义字典
count=0;#定义计数器
with open(rFile,'r') as r:
for line in r:
if line.strip()!='':
line=line.strip('\n');#去除换行符
contDict[count]=line;
count+=1;
return contDict;
def writeFile(rFile,wFile,buildType):
""" 根据 rFile【原读取内容的路径】,wFile 生成的文件路径
buildType 文件类型 """
intType=int(buildType);
contDict=readFile(rFile);
if intType==1:
type1(contDict,wFile);
elif intType==2:
type2(contDict,wFile);
else:
raise ValueError("错误的文件类型");
def type1(cDict,wFile):
""" 生成的文件类型1 类似str1=request.getParameter("str1");"""
if cDict:
start="=request.getParameter(\"";
end="\");\n";
with open(wFile,'w') as w:
for i in cDict:
line=cDict[i];
newLine="%s%s%s%s" %(line,start,line,end);
w.writelines(newLine);
print("文件生成成功,请注意查看!");
else:
print(rFile+":内容为空!");
def type2(cDict,wFile):
""" 生成的文件类型2 类似jsonObject.put("str1",str1);"""
if cDict:
start="jsonObject.put(\"";
middle="\",";
end=");\n";
with open(wFile,'w') as w:
for i in cDict:
line=cDict[i];
newLine="%s%s%s%s%s" %(start,line,middle,line,end);
w.writelines(newLine);
print("文件生成成功,请注意查看!");
else:
print(rFile+":内容为空!");
if __name__=="__main__":
rFile="F:/read.txt";
wFile="F:/write.txt";
wFile2="F:/write2.txt";
buildType=2;
writeFile(rFile,wFile2,buildType);
目前支持2种格式:
buildType =1 ,则生成内容如下:
abc1=request.getParameter("abc1");
abc2=request.getParameter("abc2");
abc3=request.getParameter("abc3");
abc4=request.getParameter("abc4");
buildType =2,<span style="font-family: Arial, Helvetica, sans-serif;">则生成内容如下:</span>
<span style="font-family:Arial, Helvetica, sans-serif;">jsonObject.put("abc1",abc1);
jsonObject.put("abc2",abc2);
jsonObject.put("abc3",abc3);
jsonObject.put("abc4",abc4);
</span><pre name="code" class="python">
以后,碰到需要大量定义的内容,直接在read.txt文件中将变量名写上就ok了。