5. LoginAction
package com.bufoon.action;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import com.bufoon.entity.User;
import com.bufoon.service.user.UserService;
import com.opensymphony.xwork2.ActionSupport;
@Controller
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
@Resource
private UserService userService;
private String username;
private String password;
public String login(){
HttpServletRequest request = ServletActionContext.getRequest();
User user = userService.findUserByNameAndPassword(username, password);
if (user != null) {
request.setAttribute("username", username);
return SUCCESS;
} else {
return ERROR;
}
}
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;
}
}
6. Util.java
package com.bufoon.util;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.security.MessageDigest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bufoon.entity.User;
import com.bufoon.service.user.UserService;
import sun.misc.BASE64Encoder;
/**
* 通用工具类
*/
public class Util {
/**
* 对字符串进行MD5加密
*
* @param str
* @return String
*/
public static String md5Encryption(String str) {
String newStr = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
BASE64Encoder base = new BASE64Encoder();
newStr = base.encode(md5.digest(str.getBytes("UTF-8")));
} catch (Exception e) {
e.printStackTrace();
}
return newStr;
}
/**
* 判断字符串是否为空
*
* @param str
* 字符串
* @return true:为空; false:非空
*/
public static boolean isNull(String str) {
if (str != null && !str.trim().equals("")) {
return false;
} else {
return true;
}
}
}六. JSP文件
1. login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html>My JSP 'index.jsp' starting page
2. success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
==