以下是struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 是否使用开发模式 -->
<constant name="struts.devMode" value="false" />
<package name="customer" extends="struts-default" namespace="/">
<interceptors>
<!-- 注册拦截器 -->
<interceptor name="loginInterceptor" class="cn.leo.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="com.leo.web.CustomerAction" method="{1}" >
<result name="list" >/jsp/customer/list.jsp</result>
<result name="customer" >/jsp/customer/edit.jsp</result>
<result name="toList" type="redirectAction">
<param name="actionName">CustomerAction_list</param>
<param name="namespace">/</param>
</result>
</action>
<action name="UserAction_login" class="com.leo.web.UserAction" method="login" >
<result name="toHome" type="redirect">index.htm</result>
</action>
</package>
</struts>
action:
private Customer customer=new Customer();
private CustomerService service=new CustomerServiceImpl();
public String test() {
System.out.println("test...");
return Action.NONE;
}
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 = service.getAll(dc);
//5 将返回的list放入request域.转发到list.jsp显示
//ServletActionContext.getRequest().setAttribute("list", list);
// 放到ActionContext
ActionContext.getContext().put("list", list);
return "list";
}
//添加客户
public String add() throws Exception {
System.out.println("add方法:");
//1 调用Service
service.addCutomer(customer);
//2 重定向到列表action方法
return "toList";
}
//修改客户
public String update() throws Exception {
System.out.println("修改");
Customer user =service.getCustomer(customer);
ActionContext.getContext().put("customer", user);
return "customer";
}
//保存修改的客户
public String save() {
service.saveUpdate(customer);
return "toList";
}
@Override
public Customer getModel() {
// TODO Auto-generated method stub
return customer;
}
但是通过调用时就是不显示:这是为什么???搞了好久没头绪请大神指明原有:
访问以下路径:http://localhost:8080/Crm/CustmoerAction_update
报错:Message There is no Action mapped for namespace [/] and action name [UserAction_login] associated with context path [/Crm].
由于使用的是struts2的2.5版本现在只需要在struts.xml中添加以下即可以实现通配字符的调用问题:
<allowed-methods>list,add</allowed-methods>这里面写的是调用的方法名放到<action></action>标签内即可