Action类
public class HelloAction {
public String hello() {
System.out.println("Hello Struts!");
return "success";
}
}
配置 struts.xml
<struts>
<package name="hello" namespace="/hello" extends="struts-default">
<action name="HelloAction" class="com.lanou3g.hello.HelloAction" method="hello">
<result name="success" type="dispatcher">/hello.jspresult>
action>
package>
<include file="com/lanou3g/def/struts.xml">include>
<include file="com/lanou3g/dynamic/struts.xml">include>
<include file="com/lanou3g/dynamic/struts.xml">include>
<include file="com/lanou3g/test/struts.xml">include>
struts>
配置struts2的核心过滤器
在web.xml中配置:
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
/*
filter-mapping>
使用快捷键(command + shift + t)搜索StrutsP
localhost:8080/sh-struts2-01/hello/HelloAction
1.通过网址请求中的hello
2.找到对应的命名空间(网址)
3.找到后 再通过网址中的HelloAction去匹配Action标签中的name
4.匹配上,用class标签创建其类的对象
5.调用该类中的方法
6.拿到类中的方法的返回值去匹配result标签的name
7.返回值匹配上,就调用标签中的页面
指定Web应用的默认编码集
<constant name="struts.i18n.encoding" value="UTF-8">constant>
指定需要Struts2处理的请求后缀
<constant name="struts.action.extension" value="action,,">constant>
给配置文件提供热加载(更改完了不用重启服务器)
<constant name="struts.devMode" value="true">constant>
这些常量配置都可以去default.properties文件找到你要修改的配置
Action类
public class Demo02Action {
public String add() {
System.out.println("增加");
return "success";
}
public String delete() {
System.out.println("删除");
return "success";
}
public String update() {
System.out.println("修改");
return "success";
}
public String find() {
System.out.println("查询");
return "success";
}
}
配置struts.xml
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false">constant>
<package name="dynamic" namespace="/dynamic" extends="struts-default">
<action name="Demo02Action_*" class="com.lanou3g.dynamic.Demo02Action" method="{1}">
<result name="success">/hello.jspresult>
action>
package>
struts>
创建方式一
public class Demo03Action {
}
随便一个普通类都可以作为一个Action类
只需要你去配置struts.xml文件
相比于servlet减少代码的侵入性
创建方式二
public class Demo04Action implements Action {
@Override
public String execute() throws Exception {
return null;
}
}
实现Action接口
意义在于提醒你Action类中的方法该怎么写
创建方式三(常用方式)
public class Demo05Action extends ActionSupport{
}
因为该类实现了很多接口,一个接口就有一个功能
Action类
public class Demo06Action {
public String execute() {
System.out.println("测试 action标签的默认值");
return "success";
}
}
struts.xml
<struts>
<package name="test" namespace="/test" extends="struts-default">
<action name="Demo06Action">
<result>/hello.jspresult>
action>
package>
struts>
CustomerAction类
public class CustomerAction extends ActionSupport{
public String findByName() {
// 获取参数
HttpServletRequest request = ServletActionContext.getRequest();
String string = request.getParameter("cust_name");
if (StringUtils.isNotBlank(string)) {
// 创建离线对象
DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
// 添加查询条件
criteria.add(Restrictions.like("cust_name", "%"+string+"%"));
// 调用service方法
CustomerService service = new CustomerServiceImpl();
List list = service.find(criteria);
// 存域
request.setAttribute("list", list);
}
return "success";
}
}
配置struts.xml
<struts>
<package name="web" namespace="/" extends="struts-default">
<action name="CustomerAction" class="com.lanou3g.web.CustomerAction" method="findByName">
<result name="success">/jsp/customer/list.jspresult>
action>
package>
struts>
web.xml 配置核心过滤器
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
/CustomerAction
filter-mapping>
service层
public List find(DetachedCriteria criteria) {
// 开启事务
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
// 调用dao层方法
CustomerDao dao = new CustomerDaoImpl();
List list = dao.find(criteria);
// 提交事务
transaction.commit();
return list;
}
dao层
public List<Customer> find(DetachedCriteria criteria) {
Session session = HibernateUtils.getCurrentSession();
Criteria c = criteria.getExecutableCriteria(session);
List<Customer> list = c.list();
return list;
}