📄 registeraction.java
字号:
package struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import java.util.Date;
import java.util.Calendar;
//import java.io.*;
import form.RegisterForm;
import hibernate.Admin;
import hibernate.Favorites;
import spring.service.interfaces.IUserService;
public class RegisterAction extends BaseAction
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
if(!isTokenValid(request))
{
this.saveToken(request);
return new ActionForward("/login/register",true);
}
else
{
this.resetToken(request);
}
ActionErrors errors = new ActionErrors();
IUserService service = this.getUserinfoService();
RegisterForm registerForm=(RegisterForm)form;
if(service.existUsername(registerForm.getName()) )//判断用户名是否存
{
errors.add("name",new ActionMessage("register.username.invalid.error"));
this.saveMessages(request, errors);
return mapping.getInputForward();
}
//设置admin字段
Admin admin=new Admin();
admin.setName(registerForm.getName());
admin.setPassword(registerForm.getPsw());
admin.setSex(registerForm.getSex());
String year=registerForm.getYear();
String month=registerForm.getMonth();
String day=registerForm.getDay();
Calendar rightNow =Calendar.getInstance();
Integer age =new Integer(rightNow.get(Calendar.YEAR) - Integer.parseInt(year));
rightNow.set(Integer.parseInt(year),Integer.parseInt(month)-1 ,Integer.parseInt(day) );
Date birthday=rightNow.getTime();
admin.setBirthday(birthday);
admin.setAge(age);
//设置Favorites字段
Favorites favorites=new Favorites();
favorites.setHobby(registerForm.getTag().trim());
//favorites.setAdmin(admin);
Integer adminId=admin.getId();
favorites.setId(adminId);
//设置结束
try
{
admin = this.getUserinfoService().save(admin);
favorites=this.getFavoriteinfoService().save(favorites);
if(admin!=null)//注册成功
{
// request.getSession().setAttribute("admin", "注册成功!!!");
// return mapping.findForward("ok");
}
else//注册失败
{
request.setAttribute("errormsg","注册失败!!" );
return mapping.findForward("error");
}
}
catch (Exception e)
{
request.setAttribute("errormsg", e.getMessage());
return mapping.findForward("error");
}
request.removeAttribute(mapping.getAttribute());
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -