一:jsp代码
[html]
<form action="/FastFood/userInfo.do?p=addUser" method="post" name="myForm" enctype="multipart/form-data">
<table class="table" cellspacing="1" cellpadding="2" width="99%" align="center" border="0">
<tbody>
<tr>
<td class="bg_tr" height="21" colspan="2"></td>
</tr>
<tr>
<td class="td_bg" height="30" colspan="2" align="center"><b style="font-size: 16px;">新增会员信息</b></td>
</tr>
<tr>
<td colspan="2" class="td_bg">注意事项:下列带*字符为必填内容,且慎重填写。</td>
</tr>
</tbody>
</table>
<table class="table" cellspacing="1" cellpadding="2" width="99%" align="center" border="0">
<tbody>
<tr>
<th class="bg_tr" align="right" colspan="2" height="25">
<input type="button" value="" onclick="javascript:history.go(-1);" style="cursor:pointer;border:0px;width:54px;height:22px;background-image:url(admin/images/fh.jpg);">
</th>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">账户名称:<font color="red">*</font></td>
<td class="td_bg" width="55%">
<div style="float:left;"><input id="userName" name="userName" onblur="yzName2()" style="width:180px;"></div>
<div id="yzUserName" style="float:left;margin-left:6px;margin-top:3px;"></div>
</td>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">账户密码:<font color="red">*</font></td>
<td class="td_bg" width="55%">
<div style="float:left;"><input type="password" id="pwd" name="pwd" onblur="yzPwd()" style="width:180px;"></div>
<div id="yzPwd" style="float:left;margin-left:6px;margin-top:3px;"></div>
</td>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">确认密码:<font color="red">*</font></td>
<td class="td_bg" width="55%">
<div style="float:left;"><input type="password" id="Rpwd" onblur="yzRPwd()" style="width:180px;"></div>
<div id="yzRpwd" style="float:left;margin-left:6px;margin-top:3px;"></div>
</td>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">性 别:<font color="red">*</font></td>
<td class="td_bg" width="55%">
<input type="radio" id="sex" name="sex" value="1" checked>男
<input type="radio" id="sex" name="sex" value="2">女
</td>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">用户头像: </td>
<td class="td_bg" width="55%"><input type="file" name="photos" style="width:252px;"></td>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">联系电话: </td>
<td class="td_bg" width="55%"><input name="telephone" onkeypress="return regInput(this,/^[0-9-]*$/,String.fromCharCode(event.keyCode))" style="width:180px;"></td>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">电子邮件: </td>
<td class="td_bg" width="55%">
<div style="float:left;"><input id="email" name="email" onblur="yzEmail()" style="width:180px;"></div>
<div id="yzEmail" style="float:left;margin-left:6px;margin-top:3px;"></div>
</td>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">联系地址: </td>
<td class="td_bg" width="55%"><input name="address" style="width:180px;"></td>
</tr>
<tr>
<td class="td_bg" width="40%" height="26" align="right">备注信息: </td>
<td class="td_bg" width="60%">
<textarea name="u_remarks" style="width:300px;height:60px;"></textarea>
</td>
</tr>
</tbody>
</table>
<table class="table" cellspacing="1" cellpadding="2" width="99%" align="center" border="0">
<tbody>
<tr>
<td class="td_bg" align="center" colspan="2" height="28">
<input type="button" value="提交" onclick="tijiao()" style="cursor:pointer;border:0px;width:42px;height:20px;background-image:url(images/tijiao.jpg);">
<input type="reset" value="重置" style="cursor:pointer;border:0px;width:42px;height:20px;background-image:url(images/tijiao.jpg);">
</td>
</tr>
</tbody>
</table>
</form>
<form action="/FastFood/userInfo.do?p=addUser" method="post" name="myForm" enctype="multipart/form-data">
<table class="table" cellspacing="1" cellpadding="2" width="99%" align="center" border="0">
<tbody>
<tr>
<td class="bg_tr" height="21" colspan="2"></td>
</tr>
<tr>
<td class="td_bg" height="30" colspan="2" align="center"><b style="font-size: 16px;">新增会员信息</b></td>
</tr>
<tr>
<td colspan="2" class="td_bg">注意事项:下列带*字符为必填内容,且慎重填写。</td>
</tr>
</tbody>
</table>
<table class="table" cellspacing="1" cellpadding="2" width="99%" align="center" border="0">
<tbody>
<tr>
<th class="bg_tr" align="right" colspan="2" height="25">
<input type="button" value="" onclick="javascript:history.go(-1);" style="cursor:pointer;border:0px;width:54px;height:22px;background-image:url(admin/images/fh.jpg);">
</th>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">账户名称:<font color="red">*</font></td>
<td class="td_bg" width="55%">
<div style="float:left;"><input id="userName" name="userName" onblur="yzName2()" style="width:180px;"></div>
<div id="yzUserName" style="float:left;margin-left:6px;margin-top:3px;"></div>
</td>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">账户密码:<font color="red">*</font></td>
<td class="td_bg" width="55%">
<div style="float:left;"><input type="password" id="pwd" name="pwd" onblur="yzPwd()" style="width:180px;"></div>
<div id="yzPwd" style="float:left;margin-left:6px;margin-top:3px;"></div>
</td>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">确认密码:<font color="red">*</font></td>
<td class="td_bg" width="55%">
<div style="float:left;"><input type="password" id="Rpwd" onblur="yzRPwd()" style="width:180px;"></div>
<div id="yzRpwd" style="float:left;margin-left:6px;margin-top:3px;"></div>
</td>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">性 别:<font color="red">*</font></td>
<td class="td_bg" width="55%">
<input type="radio" id="sex" name="sex" value="1" checked>男
<input type="radio" id="sex" name="sex" value="2">女
</td>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">用户头像: </td>
<td class="td_bg" width="55%"><input type="file" name="photos" style="width:252px;"></td>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">联系电话: </td>
<td class="td_bg" width="55%"><input name="telephone" onkeypress="return regInput(this,/^[0-9-]*$/,String.fromCharCode(event.keyCode))" style="width:180px;"></td>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">电子邮件: </td>
<td class="td_bg" width="55%">
<div style="float:left;"><input id="email" name="email" onblur="yzEmail()" style="width:180px;"></div>
<div id="yzEmail" style="float:left;margin-left:6px;margin-top:3px;"></div>
</td>
</tr>
<tr>
<td class="td_bg" width="45%" height="26" align="right">联系地址: </td>
<td class="td_bg" width="55%"><input name="address" style="width:180px;"></td>
</tr>
<tr>
<td class="td_bg" width="40%" height="26" align="right">备注信息: </td>
<td class="td_bg" width="60%">
<textarea name="u_remarks" style="width:300px;height:60px;"></textarea>
</td>
</tr>
</tbody>
</table>
<table class="table" cellspacing="1" cellpadding="2" width="99%" align="center" border="0">
<tbody>
<tr>
<td class="td_bg" align="center" colspan="2" height="28">
<input type="button" value="提交" onclick="tijiao()" style="cursor:pointer;border:0px;width:42px;height:20px;background-image:url(images/tijiao.jpg);">
<input type="reset" value="重置" style="cursor:pointer;border:0px;width:42px;height:20px;background-image:url(images/tijiao.jpg);">
</td>
</tr>
</tbody>
</table>
</form>
[html]
//提交
function tijiao(){
if(yzName2() && yzPwd() && yzRPwd() && yzEmail()){
//location="/FastFood/userInfo.do?p=addUser"
document.myForm.submit();
}
}
//提交
function tijiao(){
if(yzName2() && yzPwd() && yzRPwd() && yzEmail()){
//location="/FastFood/userInfo.do?p=addUser"
document.myForm.submit();
}
}
二:Action代码
[java]
/**
* 后台添加会员
* @param mapping
* @param form
* @param request
* @param response
* @param pageContext
* @return
* @throws Exception
*/
public ActionForward addUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
//获得form
UserinfoForm myForm = (UserinfoForm) form;
//获得提交的文件对象
FormFile ff = myForm.getPhotos();
//获得文件字节流
byte[] b=ff.getFileData();
//获得上传的文件名
String fileName=ff.getFileName();
//获得upload文件夹的真实路径
String filePath=request.getSession().getServletContext().getRealPath("/images/photos");
//获得随机的文件名
String randomName=UUID.randomUUID().toString();
//获得上传文件的后缀名
String fix=fileName.substring(fileName.lastIndexOf("."));
//创建输出文件的输出流
FileOutputStream fos=new FileOutputStream(filePath + "/" + randomName + fix);
//获得真实的文件名
String photos = randomName + fix;
//写入输出流
fos.write(b);
//关闭
fos.close();
try {
String name = request.getParameter("userName"); //账户名称
String pwd = request.getParameter("pwd"); //账户密码
String sex = request.getParameter("sex"); //性别
String phone = request.getParameter("telephone"); //联系电话
String email = request.getParameter("email"); //电子邮件
String address = request.getParameter("address"); //联系地址
String bz = request.getParameter("u_remarks"); //备注
Userinfo userinfo = new Userinfo(
name,
pwd,
Integer.parseInt(sex),
new Date(),
photos,
address,
email,
phone,
1,
bz);
userInfoService.addUser(userinfo);
out.print("<script>alert(\"注册成功!\");location=\"/FastFood/admin/view/addUser.jsp\";</script>");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
out.print("<script>alert(\"注册失败!\");location=\"/FastFood/admin/view/addUser.jsp\";</script>");
}
return null;
}
/**
* 后台添加会员
* @param mapping
* @param form
* @param request
* @param response
* @param pageContext
* @return
* @throws Exception
*/
public ActionForward addUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
//获得form
UserinfoForm myForm = (UserinfoForm) form;
//获得提交的文件对象
FormFile ff = myForm.getPhotos();
//获得文件字节流
byte[] b=ff.getFileData();
//获得上传的文件名
String fileName=ff.getFileName();
//获得upload文件夹的真实路径
String filePath=request.getSession().getServletContext().getRealPath("/images/photos");
//获得随机的文件名
String randomName=UUID.randomUUID().toString();
//获得上传文件的后缀名
String fix=fileName.substring(fileName.lastIndexOf("."));
//创建输出文件的输出流
FileOutputStream fos=new FileOutputStream(filePath + "/" + randomName + fix);
//获得真实的文件名
String photos = randomName + fix;
//写入输出流
fos.write(b);
//关闭
fos.close();
try {
String name = request.getParameter("userName"); //账户名称
String pwd = request.getParameter("pwd"); //账户密码
String sex = request.getParameter("sex"); //性别
String phone = request.getParameter("telephone"); //联系电话
String email = request.getParameter("email"); //电子邮件
String address = request.getParameter("address"); //联系地址
String bz = request.getParameter("u_remarks"); //备注
Userinfo userinfo = new Userinfo(
name,
pwd,
Integer.parseInt(sex),
new Date(),
photos,
address,
email,
phone,
1,
bz);
userInfoService.addUser(userinfo);
out.print("<script>alert(\"注册成功!\");location=\"/FastFood/admin/view/addUser.jsp\";</script>");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
out.print("<script>alert(\"注册失败!\");location=\"/FastFood/admin/view/addUser.jsp\";</script>");
}
return null;
}