web.xml:web
<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>/*</url-pattern>
</filter-mapping>apache
在com.strutsstruts:app
package com.strutsstruts;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;jsp
public class MymyAction extends ActionSupport{
public String name;
public String pwd;
public String getName(){
return name;
}
public String getPwd(){
return pwd;
}
public void setName(String name){
this.name=name;
}
public void setPwd(String pwd){
this.pwd = pwd;
}
public String execute()throws Exception{
ActionContext context = ActionContext.getContext();
if("admin".equals(name) && ("pwd".equals(pwd))){
context.put("name", name);
context.put("pwd", pwd);
context.getSession().put("name", name);
context.getSession().put("pwd", pwd);
return SUCCESS;
}
else{
context.put("error", "用戶名或密碼錯誤,請從新登陸!");
return ERROR;
}
post
}
this
}url
NewFile.jsp:spa
<form method="post" action="Mymy.action"> //Mymy:action的名字 orm
<input type="text" name="name">xml
<input type="text" name="pwd">
<input type="submit" text="提交">
</form>
struts.xml:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="my" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="privilege" class="com.interceptor.LoginInterceptor" />
</interceptors>
<action name="Mymy" class="com.strutsstruts.MymyAction"> //Mymy:爲form表單的action的名字
<result name="success">/front/success.jsp</result> //success爲 MymyAction 的return 的字符串的名字
<result name="error">/front/error.jsp</result>
<interceptor-ref name="privilege" />
</action>
<action name="inputinput" class="com.strutsstruts.MymyAction" >
<result name="inputinput">/front/inputinput.jsp</result>
</action>
</package>
</struts>
MymyAction:
package com.strutsstruts;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class MymyAction extends ActionSupport{
public String name;
public String pwd;
public String getName(){
return name;
}
public String getPwd(){
return pwd;
}
public void setName(String name){
this.name=name;
}
public void setPwd(String pwd){
this.pwd = pwd;
}
public String execute()throws Exception{
ActionContext context = ActionContext.getContext();
if("admin".equals(name) && ("pwd".equals(pwd))){
context.put("name", name);
context.put("pwd", pwd);
context.getSession().put("name", name);
context.getSession().put("pwd", pwd);
return SUCCESS; //<result name="success">的名字
}
else{
context.put("error", "用戶名或密碼錯誤,請從新登陸!"); //<resutl name="error"> .jsp </result>的名字
return ERROR;
}
}
}
攔截器:
package com.interceptor;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginInterceptor extends AbstractInterceptor{
public String intercept(ActionInvocation invocation)throws Exception{
ActionContext actionContext = invocation.getInvocationContext();
Object name= actionContext.getSession().get("name");
if(name!=null){
return invocation.invoke();
}else{
actionContext.put("msg","您還未登陸,請先登陸!");
return Action.ERROR;
}
}
}
在struts.xml引用:
<struts>
<package name="my" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="privilege" class="com.interceptor.LoginInterceptor" />
</interceptors>
<action name="Mymy" class="com.strutsstruts.MymyAction">
<result name="success">/front/success.jsp</result>
<result name="error">/front/error.jsp</result>
<interceptor-ref name="privilege" /> //攔截器引用
</action>
<action name="inputinput" class="com.strutsstruts.MymyAction" >
<result name="inputinput">/front/inputinput.jsp</result>
</action>
</package></struts>