struts2.3版本基本搭建使用
使用环境
struts-2.3.31
jdk1.8
eclipse neon (4.6.1)
官网下载地址
目录结构
app
docs
lib
src
ANTLR-LICENSE.txt
CLASSWORLDS-LICENSE.txt
FREEMARKER-LICENSE.txt
LICENSE.txt
NOTICE.txt
OGNL-LICENSE.txt
OVAL-LICENSE.txt
SITEMESH-LICENSE.txt
XPP3-LICENSE.txt
XSTREAM-LICENSE.txt
官方Demo参考
自己的登陆搭建
web.xml 配置过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
注意:
<url-pattern>是过滤的内容
<filter-class>在2.3版本要这么写
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="zzq" extends="struts-default" namespace="/">
<!-- 登陆,登出 -->
<action name="Login" class="ben.com.action.LoginAction">
<result name="success" type="dispatcher">/jsp/system/admin.jsp</result>
<result name="error" type="dispatcher">/jsp/login/login.jsp</result>
</action>
</package>
</struts>
注意:
必须要配置一个<package name="zzq" extends="struts-default" namespace="/">
<action>标签内的name属性与jsp中传的参数相同
LoginAction.java
package ben.com.action;
import java.net.URLEncoder;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import org.apache.catalina.Session;
import org.apache.struts2.ServletActionContext;
import com.ben.pojo.Users;
import com.ben.service.UsersService;
import com.ben.utils.CookieUtils;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action {
private Users user;
private String checkCode;
private boolean autoLogin;
private String errorStr;
UsersService usersService = new UsersService();
@Override
public String execute() throws Exception {
HttpSession session = ServletActionContext.getRequest().getSession();
System.out.println(session.getAttribute("user"));
String piccode = (String) session.getAttribute("piccode");
if(piccode.equals(checkCode.toUpperCase())){
user = usersService.findUsers(user);
if(user != null){
// 添加cookie和session
CookieUtils.addCookie("userName", URLEncoder.encode(user.getUserName(), "UTF-8"), ServletActionContext.getResponse(), 99999999, "/");
CookieUtils.addCookie("password", user.getPassword(), ServletActionContext.getResponse(), 999999999, "/");
session.setAttribute("user", user);
return "success";
}else{
errorStr = "用户名或密码有误";
}
}else{
errorStr = "验证码错误";
}
return "error";
}
public Users getUser() {
return user;
}
public void setUser(Users user) {
this.user = user;
}
public String getCheckCode() {
return checkCode;
}
public void setCheckCode(String checkCode) {
this.checkCode = checkCode;
}
public boolean isAutoLogin() {
return autoLogin;
}
public void setAutoLogin(boolean autoLogin) {
this.autoLogin = autoLogin;
}
public String getErrorStr() {
return errorStr;
}
public void setErrorStr(String errorStr) {
this.errorStr = errorStr;
}
}
注意:
struts会自动封装对象
注意jsp中EL表达式与struts直接封装对象的key进行区分使用