Strut2学习——基础笔记(2) Action部分

第一讲 基础知识

Tomcat设置

Strut2学习——基础笔记(2)  Action部分 - 江南 - 筚路蓝缕 以启山林

 

设置默认jdk

Strut2学习——基础笔记(2)  Action部分 - 江南 - 筚路蓝缕 以启山林

 

将该目录下的struts.xml拷入src目录中

Strut2学习——基础笔记(2)  Action部分 - 江南 - 筚路蓝缕 以启山林

 

将该目录下的jar包拷入WEB-INF/lib

Strut2学习——基础笔记(2)  Action部分 - 江南 - 筚路蓝缕 以启山林

 

web.xml进行配置

<filter>

        <filter-name>struts2</filter-name>

        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

  </filter>

 <filter-mapping>

      <filter-name>struts2</filter-name>

       <url-pattern>/*</url-pattern>

  </filter-mapping>

开发模式:

struts.xml

<constant name="struts.devMode" value="true" />  

对某些信息更改时不必重新发布

查看jar文件源码

右键点击jar文件后Properties Java Source Attachment   点击External Folder 进行设置

Strut2学习——基础笔记(2)  Action部分 - 江南 - 筚路蓝缕 以启山林

 

xml文档语法的提示

解压lib下的struts2-core-2.X.X.jar  (我解压了struts2-core-2.3.14.jar)

Strut2学习——基础笔记(2)  Action部分 - 江南 - 筚路蓝缕 以启山林

 

Strut2学习——基础笔记(2)  Action部分 - 江南 - 筚路蓝缕 以启山林

 

Struts2将请求与视图分开,增加可扩展性和灵活性

第二讲 Namespace

Namespace为空,意味着只要找到一个nametest1action未找到与他精确对应的package的时候,全都交给namespace为空的package处理,这意味它囊括了其他package处理不了的action。(处理action时 先找对应namespacepackage中有没有,没有的话去找namespace为空的package中有没有)

<package name="main" namespace="" extends="struts-default">

        <action name="test1">

            <result name="success">

                /MyJsp.jsp

            </result>

        </action>

    </package>

    <package name="back" namespace="/back" extends="struts-default">

        <action name="test1">

            <result name="success">

                /Namespace.jsp

            </result>

        </action>

</package>

访问http://localhost:8888/Struts2_0200_Namespace/ba/test1 ,响应MyJsp.jsp

访问http://localhost:8888/Struts2_0200_Namespace/back/test1,响应Namespace.jsp

拷贝项目之后注意修改web context-root

Strut2学习——基础笔记(2)  Action部分 - 江南 - 筚路蓝缕 以启山林

 

拷贝可能出现的错误

JRE system library路径问题,将其删掉,重新添加

项目右键 — Build path — configure build path — add library  — JRE system library  — next — workspace default JRE — finish

三、Action

Strut2学习——基础笔记(2)  Action部分 - 江南 - 筚路蓝缕 以启山林

 

Jsp默认编码问题,解决乱码

Action中未配置class执行  actionSupport

具体视图的返回可以由用户自己定义的Action来决定

具体的手段根据返回的字符串找到对应的配置项,来决定视图的内容

具体Action的实现可以是一个普通的java类,里面有public String execute方法即可

或者实现Ation接口

不过最常用的是从ActionSupport继承,好处在于可以直接使用struts2封装好的方法

四、路径问题说明

truts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。

虽然可以用redirect方式解决,但redirect方式并非必要。

解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)

绝对路径使用:

  ①

使用自动生成的

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

链接写成 <a href="<%=basePath%>index.jsp">index.jsp</a>

  ②

添加

<head>

<base href="<%=basePath%>">

</head>

链接写成 <a href="index.jsp">index.jsp</a>

五、DMI 动态方法调用

Action执行的时候并不一定要执行execute方法
可以在配置文件中配置Action的时候用method=来指定执行哪个方法 也可以在url地址中动态指定(动态方法调用DMI)(推荐)

<a href="/Struts2_0500_ActionMethod/user/userAdd">添加用户</a>

<a href="/Struts2_0500_ActionMethod/user/user!add">添加用户</a>(推荐)

request.getContextPath()Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI.

六、通配符配置

<action name="*_*" class="com.struts2.action.{1}Action" mothed=“{2}”>

            <result >/{1}_{2}success.jsp</result>       

        </action>

使用通配符,将配置量降到最低

一定要遵守“约定优于配置”的原则

精确匹配原则,若都含*号,按前后顺序匹配

七、AcctionAttrparamInput

使用action属性接收参数<a href="user/user!add?name=a&age=8">添加用户</a>

 、DomainModle接收参数(常用)

<a href="user/user!add?user.name=a&user.age=8">添加用户</a>

  过程:  setUser().setname()先设好user再对name进行设置

附:数据传输对象

    使用UserDTO来创建User ,即new User(UserDTO userDTO);

九 、ModelDrivenParamInput

             Action实现ModelDriven接口,getmodel()方法获取一个模型对象

MVC  action充当控制器

十 中文问题

 <constant name="struts.devMode" value="true" />

   <constant name="struts.i18n.encoding" value="GBK"/>

      <package name="user" extends="struts-default" namespace="/user">

文档的查阅:自己去查阅相关的文档

十一 简单的数据验证

    

  读源码:按照一条线来进行(页面->控制器->数据库)

 使用addFieldError方法和s:fieldError标签简单处理数据数据检验

页面开始添加 <%@taglib uri="/struts-tags" prefix="s" %>

<body>

    User Add Error!

    <s:fielderror fieldName="name" theme="simple"/>

    <br/>

    <s:property value="errors.name[0]"/>

<s:debug></s:debug>

  </body>

UserActionadd方法

this.addFieldError("name","name is error");

例如:   value=”a” 获取Action类的成员属性a的值

         Value=”errors” 获取errors属性的对象Map值

         Value=”errors.name”获取errors属性的对象Map的key为name的value

         Value=”errors.name[0] 获取errors属性的对象Map的key为name的value的第一个元素的值。

注:如果获取Action Context中的Key值,需要在前面加#(井号)

     例如:<s:property value=”#request”/>

Action Context

十二 、 访问web元素 (requestsessionapplication

提供机制访问web元素

ActionContext是一个(sredlocatl)?  是一个线程名字与它的值 

Action实现RequestAware,SessionAware, ApplicationAware (常用)

IoC设计思想(DI

DI  dependency injection 依赖注入

IoC inverse of control   控制反转

十三、Struts2的模块包含

Struts.xml 中:

<struts>

    <constant name="struts.devMode" value="true" />

    <include file="login.xml" />

</struts>

Login.xml中:

<struts>

    <package name="login" extends="struts-default" namespace="/login">

        <action name="login*" class="com.bjsxt.struts2.user.action.LoginAction{1}">

            <result>/user_login_success.jsp</result>

        </action>

    </package>

</struts>

十四、默认Action

  <default-action-ref name="index" /> 当找不到相应的action时,跳转到默认的action

十五、Action总结

1. 实现一个Action的最常用方式:从ActionSupport继承  Struts2_0300_Action

2. DMI动态方法调用   Struts2_0500_ActionMethod

3. 通配符配置 * {1} {2}    Struts2_0600_ActionWildcard

a) *_*  

4. 接收参数的方法(一般用属性或者DomainModel来接收) 0800_DomainModelParamInput

5. 简单参数验证addFieldError  Struts2_1100_SimpleDataVailation

a) 一般不使用Struts2UI标签

6. 访问Web元素 Struts2_1200_AccessWebElement

a) Map类型

i. IoC

ii. 依赖Struts2

b) 原始类型

i. IoC

ii. 依赖Struts2

7. 包含文件配置 Struts2_1300_IncludeModules

8. 默认action处理 Struts2_1400_DefaultAction

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值