Struts2-3的独有特性与StrutsPrepareAndExecute过滤器源码分析

Terwer Struts2评论167字数 1095阅读3分39秒阅读模式

action 后缀

action​ 会自动加上 .action​ ,例如文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

<form action="login">
</form>

文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

<form action="login.action">
</form>

效果一样。文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

Struts2-3的独有特性与StrutsPrepareAndExecute过滤器源码分析文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

其他类型转换

LoginAction 新增两个属性文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

private Integer age;
private Date date;

public Integer getAge() {
    return age;
}

public void setAge(Integer age) {
    this.age = age;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

login.jsp文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

年龄:<input type="text" name="age" /><br />
日期:<input type="text" name="date" /><br />

result.jsp文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

年龄:${requestScope.age }<br />
日期:${requestScope.date }<br />

效果文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

Struts2-3的独有特性与StrutsPrepareAndExecute过滤器源码分析文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

Struts2 的基本执行流程

  1. 用户浏览器输入入口页面地址。例如:http://localhost:8080/struts23/login.jsp
  2. 输入表单信息之后,点击提交,通过 form 的 action,进行对应跳转。
    <form action="login.action">
    
  3. 我们这里是 login,那么,就会去 struts.xml 寻找 name="login" 的 action 节点
    <package name="struts2" extends="struts-default">
    <action name="login" class="space.terwer.struts23.LoginAction">
        <result name="success">/result.jsp</result>
    </action>
    </package>
    

    找到之后,实例化 class 配置的 Action 处理类。文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

    然后执行对应的 setXXX 方法进行属性赋值,接着执行 Action 里面的 execute 方法:文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

    public String execute() {
    return "success";
    }
    
  4. 执行完成之后,根据返回值去 struts.xml 找到对应的跳转视图,我们这里是 result.jsp,然后显示最终结果。

    Struts2-3的独有特性与StrutsPrepareAndExecute过滤器源码分析文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

注意:Struts2 每次请求都会生成一个 Action 的对象。这与 Servlet 不同,Servlet 是每个请求共享一个对象,Struts2 的 Action 是每次请求都生成一个新对象。文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

Struts1 的处理方式与 Servlet 类似。文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

文章源自浅海拾贝-https://blog.terwergreen.com/struts25-s-unique-features-and-strutspreparendexecute-filters-source-code-analysis-dmout.html

相关文章
历史上的今天
10月
30
  • 扫码加我微信
  • 验证消息请输入:来自你的博客
  • weinxin
  • 我的微信公众号
  • 微信扫一扫与我交流吧
  • weinxin
Terwer
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: