这一周在思途教育实训,再次巩固了之前学习过的javaMVC的知识,接触了不少新的知识,比如SSM/SSH框架、Spring、Bootstrap框架、JSTL等等。也暴露出了很多问题,谨以此记录下来以便提醒自己。
<c:forEach items="${stus}" var="stu">
<tr>
<td><input style="width: 20px;" type="text" value="${stu.id}" readonly="readonly"/></td>
<td>${stu.name}</td>
<td>${stu.sex }</td>
<td>${stu.age }</td>
<td>
<button οnclick="window.open('update.jsp?id=${stu.id}')" class="btn btn-primary" id="updateBtn" name="id"
type="submit" value="${stu.id}">修改</button>
<button οnclick="window.open('delete.jsp?id=${stu.id}&name=${stu.name}&sex=${stu.sex}&age=${stu.age}')" class="btn btn-primary" id="deleteBtn"
type="button">删除</button>
</td>
</tr>
</c:forEach>
在JSTL中有<c:foreach></c>标签以实现元素的迭代。在上面的代码中,我想使用URL传参的方式实现不同页面间参数的传递,如果使用URL进行参数传递,需要在前端进行编码,并在后端进行解码(方法略),如果不对中文字符进行编码和解码的处理,在后端接受时就会出现乱码。但是在这里,参数是以EL表达式的形式给出的,如何进行编码的处理???
我首先想到的是前端编码:
encodeURI(encodeURI(“***”))
后端解码:
String str = new String(request.getParameter("param").getBytes("iso8859-1"),"UTF-8");
运行结果如下图,在后端接收的数据还是乱码。
后来看到论坛里一个神的回复(下面第一个链接),受益匪浅。从而得出了正确的处理方式:
无需对EL表达式进行编码,直接在后台获取时用URLDecoder.decode()进行解码即可。
代码:
int id=Integer.parseInt(request.getParameter("id")) ;
String name2=java.net.URLDecoder.decode(request.getParameter("name"));
String sex2=java.net.URLDecoder.decode(request.getParameter("sex"));
//String name=new String(request.getParameter("name").getBytes("iso-8859-1"),"UTF-8");
//String sex=new String(request.getParameter("sex").getBytes("iso-8859-1"),"UTF-8");
int age=Integer.parseInt(request.getParameter("age"));
运行结果如图:
成功解决问题!
参考资料:http://bbs.csdn.net/topics/250056412
http://blog.csdn.net/u010648555/article/details/50011987