Struts2-2.5.5版本是目前為止最新的版本了,相對於之前的2.3版本以及再之前的版本而言,新版本改動了很多。
好了,廢話不多說,GO CODE!
基本jar包:

web.xml核心配置,這裡要注意咯!

<!-- struts2核心控制器 -->
<filter>
<filter-name>struts2</filter-name>
<!-- 這裡要格外注意咯 -->
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- 設置struts2默認編碼集為UTF-8 -->
<init-param>
<param-name>struts.il8.encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!-- 設置struts.xml文件位置 -->
<init-param>
<param-name>filterConfig</param-name>
<param-value>classpath:struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
web.xml
接下來是struts.xml

<struts>
<!-- 設置默認編碼集為UTF-8 -->
<constant name="struts.il8n.encoding" value="UTF-8" />
<!--設置開發者模式 -->
<constant name="struts.devMode" value="true" />
<!-- 當 struts的配置文件修改後,系統是否自動重新加載該文件,默認值為false(生 產環境下使用),開發階段最好打開 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!--設置主題 -->
<!-- <constant name="struts.ui.theme" value="simple" /> -->
<!-- 該 屬性指定需要Struts 2處理的請求後綴,該屬性的默認值是action,即 所有匹配*.action的請求都由Struts 2處理。如
果用戶需要指定多個請求後綴,則多個後綴之間以英文逗號(,)隔開 -->
<constant name="struts.action.extension" value="action," />
<!-- 設置是否支持動態方法調用,true為支持,false不支持. -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<!-- 設置浏覽器是否緩存靜態內容,默認值為true(生產環境下使用),開發階段最好關閉 -->
<constant name="struts.serve.static.browserCache" value="false" />
<constant name="struts.convention.default.parent.package" value="common"/>
<!-- 公共package -->
<package name="common" namespace="/" extends="struts-default">
<!-- 配置攔截器 -->
<interceptors>
<!-- 注冊攔截器 -->
<interceptor name="loginInterceptor" class="com.Struts2Dashboard.action.LoginInterceptor" />
<!-- 引用已經注冊了攔截器,形成自定義攔截器棧,自定義攔截器棧,將覆蓋到默認棧 ,必須手動再自定義攔截器棧中 引用默認棧 -->
<interceptor-stack name="loginStack">
<!-- 默認 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<!-- 自定義 -->
<interceptor-ref name="loginInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 修改當前<package>默認棧,修改後package下的所有的action都將使用此棧 -->
<default-interceptor-ref name="loginStack"></default-interceptor-ref>
<!-- 全局結果集 -->
<global-results>
<!-- 沒有登陸 -->
<result name="noLogin">/WEB-INF/pages/sweet/404.jsp</result>
</global-results>
</package>
</struts>
struts.xml
可有可無,不要怕麻煩,前期是為了後期維護做准備的。
bean就不在粘貼了哈,需要的話,可以評論留言哈。

/**
* 登錄攔截器action
*/
@SuppressWarnings("serial")
public class LoginInterceptor implements Interceptor {
//初始化
public void init() {
}
//攔截
public String intercept(ActionInvocation invocation) throws Exception {
/*if(LoginAction.class == invocation.getAction().getClass()){
return invocation.invoke();
}*/
Object action = invocation.getAction();
if (action instanceof LoginAction) {
//如果是LoginAction,則進行執行,即不做攔截
return invocation.invoke();
}
//判讀session是否存在用戶
User user = (User) ActionContext.getContext().getSession().get("user");
System.out.println(user);
if(user == null){
System.out.println("action");
return "noLogin";
}
//放行
return invocation.invoke();
}
//銷毀
public void destroy() {
}
}
LoginInterceptor

public class LoginAction extends ActionSupport implements ModelDriven<User> {
private User user = new User();
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
@Action(value="login")
public String execute() throws Exception {
ActionContext actionContext = ActionContext.getContext();
if ("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())) {
actionContext.getSession().put("user", user);
System.out.println("success");
return "success";
} else {
System.out.println("input");
return "input";
}
}
}
LoginAction
只是部分關鍵性代碼,需要項目的話,評論處留下聯系方式。
沒有考慮session過期的情況哈,只是最基礎的登錄攔截器而已,請多多指教謝謝。
轉載請注明出處,謝謝!未經授權請勿私自對本文相關圖片進行添加水印!