Struts2使用原理笔记(Spring整合)

把请求和展示分开,实现可扩展性
这里写图片描述
1.显示员工信息
①在index.jsp中:

<a href="emp-list">list all Employees</a>

②在Struts2.xml中:

<action name="emp-*" class="employeeAction" method="{1}">
<result name="list">/WEB-INF/views/emp-list.jsp</result><!-- 显示全部员工信息 -->
</action>

③在applicationContext-beans.xml中:(依赖注入)

<bean id="employeeAction" class="com.atguigu.ssh.actions.EmployeeAction"
    scope="prototype">
    <property name="employeeService" ref="employeeService"></property>
</bean>
<bean id="employeeService" class="com.atguigu.ssh.service.EmployeeService">
        <property name="employeeDao" ref="employeeDao"></property>
</bean>
<bean id="employeeDao" class="com.atguigu.ssh.dao.EmployeeDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

④在EmployeeAction.java中:

private EmployeeService employeeService;

    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    private Map<String, Object> request;

    public void setRequest(Map<String, Object> arg0) {
        this.request = arg0;
    }

    public String list() {
        request.put("employees", employeeService.getAll());
        return "list";// 对应Struts.xml配置文件中action标签中result name="list"
    }

⑤在EmployeeService.java中:

private EmployeeDao employeeDao;

    public void setEmployeeDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }
    public List<Employee> getAll(){
        List<Employee> employees= employeeDao.getAll();
        return employees;
    }

⑥在EmployeeDao.java中:

public  List<Employee> getAll(){
        String hql = "FROM Employee e LEFT OUTER JOIN FETCH e.department";
        List<Employee> le =getSession().createQuery(hql).list();
        return getSession().createQuery(hql).list();
    }

这里的getSession()是BaseDao中定义的,所以EmployeeDao类需要继承BaseDao类
这里使用hibernate的hql语句,所以需要用到POJO类
⑦在BaseDao.java中:

private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public Session getSession() {
        return this.sessionFactory.getCurrentSession();
    }

⑧在Employee类中:

public class Employee {

    private Integer id;
    // 不能被修改
    private String lastName;
    private String email;
    // 从前端传入的是String类型,所以需要注意转换
    private Date birth;

    // 不能被修改
    private Date createTime;
    // 单向n-1的关联关系
    private Department department;//直接定义外键对应的表名对应的实体类

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", lastName=" + lastName + ", email="
                + email + ", birth=" + birth + ", createTime=" + createTime
                + ", department.id=" + department.getId() + "]";
    }

}

对应数据库表中的employee表
这里Department类对象对应department表,其为employee表的外键和department表的主键

namespace决定了action的访问路径,默认为”“

拷贝一个项目到工程文件的时候,一定要修改Web Context-root,使其与修改的项目名相同

关于Action类,Struts1是单例模式,会出现线程同步的问题,而对于Struts2,每个人访问时都会new一个新的Action对象

ActionSupport类在swork jar包中

Struts2中的路径问题是根据Action的路径而不是jsp的路径来确定,如果写的是jsp的路径(相对路径),那么会报错,因为服务器会判断ction的路径下是否有该jsp文件。解决办法:统一使用绝对路径/ssh-2/index.jsp,即从根目录写起

动态方法调用DMI,不用Action的method属性,可以使用通配符”*”,method={1}

<s:debug>标签显示debug内容

IoC 控制反转:在Action类中添加setter方法,可以直接设置jsp传过来的参数HttpServletRequest,将其转变为Map。本来自己控制,现在给容器控制——(Spring将这种思想发扬光大)

include标签,添加Struts2-xx.xml文件到Struts2.xml,使得模块化<include file="Struts2-xx.xml">

<default-action-ref name="index" />默认Action路径

dispatcher 跳转; redirect 重定向;chain: Action之间跳转,浏览器地址是最初的Action;redirectAction: Action之间跳转,浏览器地址是最后的jsp

OGNL:传对象参数时,注意jsp界面使用user.username和user.password

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值