一:填写注册内容
这块儿除了原来写的,还有头像的选择
<s:form action="userInfo_addUserInfo">
<tr >
<td width="73" height="30" bgcolor="F9F9F9">用户名</td>
<td width="288" height="30" align="left" bgcolor="#FFFFFF"><s:textfield name="account"/><s:fielderror><s:param value="%{'account'}"/></s:fielderror></td>
<td width="82" height="30">真实姓名</td>
<td width="303" height="30" align="left" bgcolor="#FFFFFF"><s:textfield name ="realname"/><s:fielderror><s:param value="%{'realname'}"/></s:fielderror></td>
这些没什么可说的,
每个后边跟自己的错误定义 比都放在一起好:<s:fielderror><s:param value=></s:fielderror>
这是头像的选择:
做成单选按钮,一起传参给action
<td height="72"><img src="images/headgif/1.gif" width="60" height="60">
<br><br>
<input name="headgif" type="radio" value="images/headgif/1.gif" checked class="button"></td>
<td><img src="images/headgif/2.gif" width="60" height="60">
<br> <br>
<input type="radio" name="headgif" value="images/headgif/2.gif" class="button"></td>
注册也有上一次分析的校验码:
不同的验证码主要还是image.jsp
<tr>
<td height="30" bgcolor="F9F9F9">校验码</td>
<td height="30" colspan="3" align="left" bgcolor="#FFFFFF"><s:textfield name ="code"/><s:fielderror><s:param value="%{'code'}"/></s:fielderror></td>
</tr>
<tr >
<td height="30" bgcolor="#FFFFFF"></td>
<td height="30" colspan="3" align="left" bgcolor="#FFFFFF"><a href="javascript:refreshImg('validateCodeImg');"><img src="image.jsp" name="validateCodeImg" border=0 id="validateCodeImg" /></a></td>
</tr>
二:action,创建用户名对应的域名
还是先validate
public void validateAddUserInfo() {
hql = "from UserInfo where account= '" + userInfo.getAccount()+ "'";
if (null != objectDao.queryFrom(hql)) {
this.addFieldError("account", "用户名重复,请重新输入!");
}
检查用户名是否唯一
检查完之后,把md5信息加密后的密码覆盖进去
userInfo.setPassword(com.mr.tools.ValidateExpression.encodeMD5(userInfo
.getPassword()));
userInfo.setHomepage(userInfo.getHomepage() + userInfo.getAccount());
boolean flag = objectDao.saveT(userInfo);
如果以上都成功,那就准备给用户分配地址
if (flag) {
String descPath = ServletActionContext.getRequest().getRealPath("/" + userInfo.getAccount() + "");
String sourPath = ServletActionContext.getRequest().getRealPath("/templet/index.jsp");
if (com.mr.tools.FileOperation.buildJSP(sourPath, descPath,userInfo.getAccount())) {
result = "您注册成功!";
request.getSession().setAttribute("freeze",userInfo.getFreeze());
request.getSession().setAttribute("account",userInfo.getAccount());
}
}
这块儿比较绕:
把用户名取出来,然后用getRealPath()用于返回绝对路径,即使这是 /用户名不存在,也能返回地址
然后是取出早就预存好的模版的地址,交给bulidJsp处理
public static boolean buildJSP(String souPath, String desPath, String title) {
FileInputStream fileinputstream = null;
byte[] bytes = null;
try {
fileinputstream = new FileInputStream(souPath);
bytes = new byte[1024 * 5]; //5k缓存
fileinputstream.read(bytes);
fileinputstream.close();
String templateContent = new String(bytes);
File file = new File(desPath);
if(!file.exists()){ //没有则创建
file.mkdir();
}
desPath=desPath+"/index.jsp"; //用户名 + '/index.jsp
FileOutputStream fileoutputstream = new FileOutputStream(desPath);
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes); //创建路径
fileoutputstream.close();
return true;
} catch (Exception e) {
System.out.println(e);
return false;
}
}
绕了一大圈,总算把用户名对应的的文件夹和jsp文件创建好了
三:用户名/index.jsp中的内容
<%
String path1=request.getServletPath();
path1 = path1.substring(1);
path1=path1.substring(0, path1.indexOf("/"));
response.sendRedirect("userInfo_goinUser.htm?account="+path1+"");
%>
Substring(1)是用来去掉传过来字串里的头一个标记“/”
然后截取到下一个“/”,
最后重定向过去