<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="stuInfo" class="com.model.stuInfo"></jsp:useBean>
<jsp:setProperty property="*" name="stuInfo" />
<jsp:useBean id="stuServer" class="com.service.stuInfoService"></jsp:useBean>
<%
if(stuServer.addStu(stuInfo))
out.print("success");
else
out.print("failure");
%>
</body>
使用如上事例处理表单时,
使用jsp的useBean指令的setProperty 指令 ,使用 property="*"
会将前端 请求参数 (此处为表单传过来的表单数据)按照javabean的属性名对应,注入到声明的实例对象stuInfo中,如果属性名
于前端表单的属性名不对应,则跳过该属性名数据的注入。
前端表单如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'inputstuinfo.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<jsp:include page="isLogin.jsp"></jsp:include>
<body>
<center>
<form action="inputStuInfo_result.jsp" method="post">
<table bgColor="#c0c0c0">
<tr>
<td align="center" bgcolor=green colspan=2><font color=white>学生信息录入</font></td>
</tr>
<tr>
<td>昵称:</td>
<td><input type="text" size=20 name="nicheng"></td>
</tr>
<tr>
<td>真实姓名:</td>
<td><input type="text" size=20 name="truename"></td>
</tr>
<tr>
<td>性别</td>
<td><input type="radio" name="xb" checked value="1">男
<input type="radio" name="xb" value="0">女</td>
</tr>
<tr>
<td>出生日期:</td>
<td><input type="text" name="csrq" size=20></td>
</tr>
<tr>
<td>所学专业:</td>
<td><select name="zy">
<option>计算机科学与技术</option>
<option selected>通信工程</option>
<option>电气工程</option>
</select></td>
</tr>
<tr>
<td>所学课程:</td>
<td><select name="kc" size=2 multiple="multiple">
<option>计算机导论</option>
<option>数据结构</option>
<option>数据库原理</option>
<option>操作系统</option>
<option>计算机网络</option>
<option>软件工程</option>
</select></td>
</tr>
<tr>
<td>兴趣</td>
<td><input type="checkbox" checked value="music" name="xq">听音乐
<input type="checkbox" checked value="book" name="xq">看小说 <input
type="checkbox" value="web" name="xq">上网 </td>
</tr>
<tr valign="top">
<td>备注</td>
<td><textarea rows=3 cols=20 name="bz"></textarea></td>
</tr>
<tr ><td align="center" colspan="2" ><input type="submit" value="提交"> <input
type="reset" value="重置"></td></tr>
</table>
</form>
</center>
</body>
</html>
其中,<select name="kc" size=2 multiple="multiple">
和<input type="checkbox" checked value="music" name="xq">听音乐
多选框,多选项卡 选中的数据将当作 字符串数组的形式 传入inputStuInfo_result.jsp处理页面,前提是前端表单该控件的name属性必须一致,形如:
<input type="checkbox" checked value="music" name="xq">听音乐
<input type="checkbox" checked value="book" name="xq">看小说
,否则将无法加入该控件的字符串数组 传入后端表单的处理程序。