<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 指定struts2是否以开发模式运行
1.热加载主配置.(不需要重启即可生效)
2.提供更多错误信息输出,方便开发时的调试
-->
<constant name="struts.devMode" value="true"></constant>
<package name="crm" namespace="/" extends="struts-default" >
<interceptors>
<!-- 注册拦截器 -->
<interceptor name="loginInterceptor" class="cn.web.interceptor.LoginInterceptor"></interceptor>
<!-- 注册拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor">
<param name="excludeMethods">login</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 指定包中的默认拦截器栈 -->
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<!-- 定义全局结果集 -->
<global-results>
<result name="toLogin" type="redirect" >/login.jsp</result>
</global-results>
<global-exception-mappings>
<!-- 如果出现java.lang.RuntimeException异常,就将跳转到名为error的结果 -->
<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
<action name="CustomerAction_*" class="cn.web.action.CustomerAction" method="{1}" >
<result name="list" >/jsp/customer/list.jsp</result>
<result name="toList" type="redirectAction">
<param name="actionName">CustomerAction_list</param>
<param name="namespace">/</param>
</result>
</action>
<action name="UserAction_*" class="cn.web.action.UserAction" method="{1}" >
<result name="toHome" type="redirect" >/index.htm</result>
<result name="error" >/login.jsp</result>
</action>
</package>
</struts>
拦截器代码:
public class LoginInterceptor extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
//1.获得session
Map<String, Object> session = ActionContext.getContext().getSession();
//2.获得登录标识
Object object = session.get("user");
//3.判断登录标识是否存在
if(object == null){
return "toLogin";
}else{
return invocation.invoke();
}
}
}
Action代码1:
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
private CustomerService cs = new CustomerServiceImpl();
private Customer Customer = new Customer();
public String list() throws Exception{
//1 接受参数
String cust_name = ServletActionContext.getRequest().getParameter("cust_name");
//2 创建离线查询对象
DetachedCriteria dc =DetachedCriteria.forClass(Customer.class);
//3 判断参数拼装条件
if(StringUtils.isNotBlank(cust_name)){
dc.add(Restrictions.like("cust_name", "%"+cust_name+"%"));
}
//4 调用Service将离线对象传递
List<Customer> list = cs.getAll(dc);
//5 将返回的list放入request域.转发到list.jsp显示
//ServletActionContext.getRequest().setAttribute("list", list);
//5将返回的list放入ActionContext域。转发到list.jsp显示
ActionContext.getContext().put("list", list);
return "list";
}
//添加客户
public String add() throws Exception {
//1.调用service
cs.save(Customer);
//2.重定向到action方法
return "toList";
}
@Override
public Customer getModel() {
return Customer ;
}
}
Action代码2:
public class UserAction extends ActionSupport implements ModelDriven<User>{
private User user = new User();
private UserService us = new UserServiceImpl();
@Override
public String execute() throws Exception {
//1.调用service执行登录操作
User u = us.login(user);
//2.将返回的user对象放入session域作为登录标识
ActionContext.getContext().getSession().put("user", u);
//3.重定向到项目的首页
return "toHome";
}
@Override
public User getModel() {
return null;
}
}
写Action的流程:
1.先继承ActionSupport类,实现 ModelDriven<T>接口
2.重写execute方法
3.实现未实现的类
写拦截器的流程:
1.继承MethodFilterInterceptor类
2.实现未实现的doIntercept方法