struts2基础知识

阅读更多
struts.xml:








converter="converter.DateConvert">

/example/{1}.jsp
login

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 arg0) {

}

@Override
public void setSession(Map 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 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 session = arg0.getInvocationContext().getSession();
if (null != session) {
// TODO
return arg0.invoke();
}
else{
return Action.LOGIN;
}
}

}
  • struts2基础知识_第1张图片
  • 大小: 44.8 KB
  • demo-mvc-struts.zip (4.2 MB)
  • 下载次数: 0
  • 查看图片附件

你可能感兴趣的:(struts2)