struts2第一次使用+拦截器

web.xml:

<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>

在com.strutsstruts:

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;
}
else{
context.put("error", "用户名或密码错误,请重新登录!");
return ERROR;
}

}

}

NewFile.jsp:

<form method="post" action="Mymy.action">    //Mymy:action的名字  


<input type="text" name="name">

<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>

猜你喜欢

转载自www.cnblogs.com/broadencoder/p/12443940.html