struts2基础知识

struts.xml:

<interceptors>
<interceptor name="basic" class="interceptor.BasicInterceptor" />
<interceptor-stack name="default">
<interceptor-ref name="validation" />
<interceptor-ref name="timer" />
</interceptor-stack>
</interceptors>
<action name="*" class="action.BasicAction" method="{1}"
converter="converter.DateConvert">
<interceptor-ref name="default"></interceptor-ref>
<result name="input">/example/{1}.jsp</result>
<result type="redirectAction">login</result>
</action>
Action:
package action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class BasicAction extends ActionSupport implements ParameterAware,
ServletRequestAware, ServletResponseAware, SessionAware,
ApplicationAware {


@Override
public void setApplication(Map<String, Object> arg0) {

}

@Override
public void setSession(Map<String, Object> arg0) {
//ActionContext.getContext().getSession();

}

@Override
public void setServletResponse(HttpServletResponse arg0) {


}

@Override
public void setServletRequest(HttpServletRequest arg0) {
//ActionContext.getContext().get("request");
}

@Override
public void setParameters(Map<String, String[]> arg0) {

}

}
converter:
package converter;

import java.awt.List;
import java.lang.reflect.Member;
import java.util.Arrays;
import java.util.Map;

import ognl.DefaultTypeConverter;

public class DateConvert extends DefaultTypeConverter {

@SuppressWarnings("rawtypes")
@Override
public Object convertValue(Map context, Object value, Class toType) {
if (toType == List.class)
{
return Arrays.asList(value);
}
return super.convertValue(context, value, toType);
}

@SuppressWarnings("rawtypes")
@Override
public Object convertValue(Map context, Object target, Member member,
String propertyName, Object value, Class toType) {

return super.convertValue(context, target, member, propertyName, value, toType);
}

}
interceptor:
package interceptor;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")
public class BasicInterceptor extends AbstractInterceptor {

@Override
public String intercept(ActionInvocation arg0) throws Exception {
Map<String, Object> session = arg0.getInvocationContext().getSession();
if (null != session) {
// TODO
return arg0.invoke();
}
else{
return Action.LOGIN;
}
}

}

你可能感兴趣的:(struts2)