⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 useraction.java

📁 有简单的网上书店需求及设计流程
💻 JAVA
字号:
package org.wiely.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.wiely.commons.MD5Code;
import org.wiely.commons.Pager;
import org.wiely.service.BookService;
import org.wiely.service.UserService;
import org.wiely.vo.User;

import com.opensymphony.xwork2.ActionSupport;

/**
 * This is a Action for User . it extends ActionSupport and implements
 * SessionAware. so it can complete most Action work,and visit the servlet
 * object .
 * 
 * @param username
 * @param password
 * @param repassword
 * @param userService,
 *            the param is in aplicationContext.xml
 * @param map
 *            session ,the session is envloped by struts2.It can visit the
 *            servlet object
 * @param bookService
 */

@SuppressWarnings("serial")
public class UserAction extends ActionSupport implements SessionAware {

	protected UserService userService;
	protected BookService bookService;
	protected Pager pager;
	@SuppressWarnings("unchecked")
	protected Map session;

	String username;
	String password;
	String repassword;
	int currentPage = 1;

	/**
	 * validate the method of register()
	 */
	public void validateRegister() {
		System.out.println("validateRegister~~~~~~~~~~~~~~");
		if (!password.equalsIgnoreCase(repassword)) {
			addFieldError(repassword, getText("errors.repassword"));
		}
		if ((username == null) || (username.trim().length() < 2)) {
			addFieldError(username, getText("errors.username"));
		}
		if ((password == null) || (password.trim().length() < 6)
				|| (password.trim().length() > 11)) {
			addFieldError(password, getText("errors.password"));
		}
		try {
			if (userService.exitsUser(username)) {
				addFieldError("exitsname", getText("errors.exitsusername"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * validate the method of login()
	 */
	public void validateLogin() {
		if (session.get("user") == null) {
			System.out.println("validateLogin~~~~~~~~~~~~~~");
			if ((username == null) || (username.trim().length() < 2)) {
				addFieldError(username, getText("errors.username"));
			}
			if ((password == null) || (password.trim().length() < 6)
					|| (password.trim().length() > 11)) {
				addFieldError(password, getText("errors.password"));
			}
		}

	}

	/**
	 * 
	 * @return String
	 * @throws Exception
	 * @describe user login
	 */
	@SuppressWarnings("unchecked")
	public String login() throws Exception {

		if (session.get("user") == null) {
			User user = new User();
			user.setUsername(username);
			//MD5 add secret
			MD5Code  md5=new MD5Code();
			user.setPassword(md5.getMD5ofStr(password));

			User user1 = userService.checkUser(user);

			if (user1 != null) {
				session.put("user", user1);

				int totalSize = bookService.getTotalRecords();
				pager = new Pager(currentPage, totalSize);
				List books = bookService.queryAllBooks(currentPage, pager
						.getPageSize());

				HttpServletRequest request = ServletActionContext.getRequest();

				request.setAttribute("books", books);
				request.setAttribute("pager", pager);

				return SUCCESS;

			} else {
				addFieldError("usernotexits", getText("errors.user.notexits"));
				return INPUT;

			}
		} else {
			int totalSize = bookService.getTotalRecords();
			pager = new Pager(currentPage, totalSize);
			List books = bookService.queryAllBooks(currentPage, pager
					.getPageSize());

			HttpServletRequest request = ServletActionContext.getRequest();

			request.setAttribute("books", books);
			request.setAttribute("pager", pager);

			return SUCCESS;
		}
	}

	/**
	 * 
	 * @return String
	 * @throws Exception
	 * @describe new user register
	 */
	public String register() throws Exception {
		User user = new User();
		//MD5 add secret
        MD5Code md5=new MD5Code();
		user.setPassword(md5.getMD5ofStr(password));
		user.setUsername(username);

		userService.register(user);

		return SUCCESS;
	}

	/**
	 * 
	 * @return String
	 * @throws Exception
	 * @describe user logout
	 */
	public String logout() throws Exception {
		if (session.get("user") != null) {
			session.remove("user");
		}
		return SUCCESS;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	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 getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void setSession(Map session) {
		this.session = session;
	}

	public BookService getBookService() {
		return bookService;
	}

	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -