Struts2 框架简介

MVC设计模式

MVC(Model-View-Controller 模型-视图-控制器)是一个存在于服务器表达层的模型。在MVC经典架构中,强制性地把应用程序的输入、处理和输出分开,将程序分成3个核心模块——模型、视图、控制器。

在Web应用的MVC模式中存在如下划分:
Model部分:由JavaBean充当。
View部分:由JSP页面充当。
Controller部分:由Servlet充当。


Struts2 结构体系

Struts是Apache软件基金下的Jakarta项目的一部分,它目前有两个版本(Struts 1.x和Struts 2.x)都是基于MVC经典设计模式的
框架。
针对Struts 1框架中存在的缺陷与不足,诞生了全新的Struts 2框架。它修改了Struts 1框架中的缺陷,而且还提供了更加灵活与强大的功能。
Struts 2是基于WebWork技术开发的全新Web框架,其结构体系如图


Action对象

Action对象是Struts 2框架中的重要对象,主要用于处理HTTP请求。
在Struts2框架之中,表单提交的数据会自动注入到与Action对象中相对应的属性,通过Action对象为属性提供setter方法进行注入。

public class UserAction extends ActionSupport {
    // 用户名属性
    private String username; 
    // 为username提供setter方法
    public void setUsername(String username) {
        this.username = username;
    }
    // 为username提供getter方法
    public String getUsername() {
        return username;
    }
    public String execute() {
        return SUCCESS;
    }
}

配置struts.xml#

Struts 2框架中的Action对象是一个控制器的角色,Struts2框架通过它处理Http请求。其请求地址的映射需要在struts.xml文件中使用元素配置

<action name="userAction" class="com.wgh.action.UserAction" method="save">
     <result>success.jsp</result>
</action>
属性说明
name用于配置Action对象被请求的URL映射(理解为Action方法的返回值对应的)
class指定Action对象的类名
method设置请求Action对象时调用该对象的哪一个方法
converter指定Action对象类型转换器的类

使用通配符简化配置

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="*Action" class="com.wgh.action.{1}Action">
            <result name="success">result.jsp </result>
            <result name="update">update.jsp</result>
            <result name="del">result.jsp</result>
        </action>
    </package>
</struts>

参考代码

public class FundAction extends ActionSupport {

    private int id;
    private Fund fund;
    private FundDao fundDao = new FundDaoHibernateImpl();
    private List<Fund> funds;

    /**
     * 新增数据
     *
     * @return
     */
    public String add() {
        fundDao.insert(fund);
        return "success";
    }

    /**
     * 根据id删除数据
     *
     * @return
     */
    public String delete() {
        fundDao.delete(id);
        return "success";
    }

    /**
     * 更新数据由fund
     *
     * @return
     */
    public String update() {
        fundDao.update(fund);
        return "success";
    }

    /**
     * 传入id转至update
     *
     * @return
     */
    public String startUpdate() {
        fund = fundDao.findById(id);
        return "startUpdate";
    }

    /**
     * 获取fund表所有数据
     *
     * @return
     */
    public String show() {
        funds = fundDao.findAll();
        ((Map) ActionContext.getContext().get("request")).put("fundList", funds);
        return "showFunds";
    }

    public int getId() {
        return id;
    }

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

    public Fund getFund() {
        return fund;
    }

    public void setFund(Fund fund) {
        this.fund = fund;
    }

    public FundDao getFundDao() {
        return fundDao;
    }

    public void setFundDao(FundDao fundDao) {
        this.fundDao = fundDao;
    }

    public List<Fund> getFunds() {
        return funds;
    }

    public void setFunds(List<Fund> funds) {
        this.funds = funds;
    }

struts.xml

<?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>

    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
    <!-- 配置文件修改了,不需要重启服务器-->
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- jsp中不使用struts的样式 -->
    <constant name="struts.ui.theme" value="simple" />
    <!-- Action所在的包的后缀 -->
    <constant name="struts.convention.package.locators" value="action,actions,controller,struts,struts2"/>

    <package name="fund" namespace="/fund" extends="struts-default">
        <!-- 动态方法调用测试 -->
        <action name="fundAction" class="com.mybank.fundtrans.controller.FundAction">
            <result name="login">/login.jsp</result>
            <result name="index" type="redirect">/index.jsp</result>
        </action>
        <!-- 使用通配符简化配置 -->
        <action name="*Fund" class="com.mybank.fundtrans.controller.FundAction"
                method="{1}">
            <result name="startUpdate">/fund/fund_update.jsp</result>
            <result name="showFunds">/fund/fund_list.jsp</result>
            <result name="success" type="chain">showFund</result>
        </action>
        <!-- 用于链接请求 -->

        <action name="*">
            <result name="success">/fund/{1}.jsp</result>
        </action>
    </package>

    <!-- Add packages here -->

</struts>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值