package com.sanqing.action;
import java.net.URLDecoder;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.sanqing.po.Users;
import com.sanqing.service.UsersService;
public class UserWebManagerAction {
private UsersService usersService;//业务逻辑层
//用户
private Users users;
//消息提示和跳转
private String message;
private String dirAddr;
//用户
private Integer userId;
private String username;
private String password;
private String name;
private String sex;
private String address;
private String phone;
private String post;
private String email;
private String regTime;
private String regIpAddress;
public UsersService getUsersService() {
return usersService;
}
public void setUsersService(UsersService usersService) {
this.usersService = usersService;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getDirAddr() {
return dirAddr;
}
public void setDirAddr(String dirAddr) {
this.dirAddr = dirAddr;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getRegTime() {
return regTime;
}
public void setRegTime(String regTime) {
this.regTime = regTime;
}
public String getRegIpAddress() {
return regIpAddress;
}
public void setRegIpAddress(String regIpAddress) {
this.regIpAddress = regIpAddress;
}
/**
* 显示用户信息
* @return
* @throws Exception
*/
public String list() throws Exception {
Users user = (Users)ActionContext.getContext().getSession().get("user");
if(user!=null){
users = user;
return "lists";
}else{
users = null;
message="对不起,您还未登录!!";
dirAddr=ServletActionContext.getRequest().getContextPath()+"/login.jsp";
return "Failed";
}
}
/**
* 修改用户信息
* @return
* @throws Exception
*/
public String update() throws Exception
{
Users user = usersService.queryByID(userId);
if(user!=null){
user.setAddress(address);
user.setEmail(email);
user.setName(name);
user.setPhone(phone);
user.setPost(post);
user.setRegIpAddress(regIpAddress);
user.setSex(sex);
if(usersService.update(user)){
message="恭喜您,完善信息成功!!";
//注意:更改后一件很重要的事,使session里面保存的用户也用这个完整的信息
ActionContext.getContext().getSession().put("user",user);
dirAddr=ServletActionContext.getRequest().getContextPath()+"/index.jsp";
return "updateSuccess";
}else{
message="对不起,完善信息失败,请重试!!";
dirAddr=ServletActionContext.getRequest().getContextPath()+"/user.jsp";
return "delFailed";
}
}
message="对不起,完善信息失败,请重试!!";
dirAddr=ServletActionContext.getRequest().getContextPath()+"/user.jsp";
return "delFailed";
}
/**
* 用户注册
* @return
* @throws Exception
*/
public String reg() throws Exception
{
Users user = new Users();
user.setPassword(password);
user.setRegTime(new Date());
user.setUsername(username);
if(usersService.save(user)){
return "nowlogin";
}else{
message="对不起,注册用户失败,请重试!!";
dirAddr=ServletActionContext.getRequest().getContextPath()+"/register.jsp";
return "Failed";
}
}
/**
* 验证用户名是否存在
* @return
* @throws Exception
*/
public String usernameExit() throws Exception
{
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
String myusername = new String(username.getBytes("ISO8859-1"),"GB2312");
if(usersService.usernameExit(myusername)){ //用户名存在
response.getWriter().print("用户名已经存在了");
}else{
}
return null;
}
}