strut2的学习笔记

struts2的使用
1:导入相应的jar包
2:在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框架来处理了

3:你的相应action类要继承ActionSupport
4:在struts.xml配置相应的action
struts.xml中的一些配置
<!-- 开发的时候配置这个有利于调试程序-->
<constant name="struts.devMode" value="true" />

<!-- 浏览器是否进行缓存处理,建议开发阶段用false -->
<constant name="struts.serve.static.browserCache" value="false"/>

<!-- 修改配置文件是否自动加载无需重启服务器 建议开发阶段用true -->
<constant name="struts.configuration.xml.reload" value="true"/>

<!--请求url后缀默认为 struts.action.extension=action,,
那么你地址后面可以加action或者什么都不加
-->
<!-- 请求url后缀的必须为action或do -->
<constant name="struts.action.extension" value="action,do"/>

<!-- 有这句namespace和action中的name值之间不允许插入其他值 -->
<constant name="struts.mapper.alwaysSelectFullNamespace" value="true"/>

在struts.xml中可以引入多个配置文件,在开发中比较常用
每个成员写自己的配置,在struts.xml引入你的配置文件
防止多个人同时操作struts.xml照成不必要的影响
 
<struts>
    <include file="xxx.xml"/>
</struts>


在页面中使用<s:set name="theme" value="'simple'" scope="page" />
就可以防止struts框架给你添加不必要的主题代码

也可指定自己指定的样式<s:set name="theme" value="%{myTheme}" scope="page" />


如果在struts.xml中配置<constant name="struts.ui.theme" value="simple"/>
那么你的每个页面都不会添加不必要的主题代码


请求重定向
<result name="success" type="redirect">
    <param name="location">/toredirect.jsp</param>
    <param name="username">abc</param>
    <param name="password">123</param>
</result>


param中的name属性的特殊值(能被框架直接识别的如:location)可以在struts核心包中org.apache.struts2.dispatcher.ServletRedirectResult
这个类中可以找到这些值
struts框架会自动将地址拼接成/toredirect.jsp?username=abc&password=123
在jsp页面直接用${param.username}就可得到参数的值abc


重定向到另一个action
<action name="redirect_action">
    <result type="redirectAction">index</result>
</action>

重定向到另外package下的action
<action name="redirect_action2">
    <result type="redirectAction">
        <param name="actionName">aaa</param>
        <param name="namespace">/test</param>
    </result>
</action>




将jsp页面直接显示在浏览器窗口,不解析页面中的标签
<action name="plain_text">
    <result type="plainText">
    <!-- name="actionName"值在 org.apache.struts2.dispatcher包下面的PlainTextResult文件中 -->
    <param name="location">/index.jsp</param>
    <!-- 指定文件的读取的编码 name="charSet"的值可在核心包org.apache.struts2.dispatcher下面的PlainTextResult文件中-->
    <param name="charSet">UTF-8</param>
    </result>
</action>


配置全局的result,那么在该package下的action中没有找到相应的result时会用这个全局的result
<package >
    <global-results>
        <result name="miss">/WEB-INF/hello.jsp</result>
    </global-results>
</package>

为action中的属性注入值(当然你可以在某个方法中直接对属性赋予相应的值)
<action name="demo1" class="cn.axin.Demo1Action">
    <!-- 为Demo1Action中的username属性(名字可以不是username)注入值(但是注意Demo1Action中一定要有setUsername方法) -->
    <param name="username">zhangsan</param>
    <result>/hello.jsp</result>
</action>

在jsp页面取出Demo1Action中username属性的值
${username}
这里username是和Demo1Action中的getUsername方法对应的,否则取不到数据


<!-- 通配符的使用 -->
<action name="good_*" class="cn.axin.ThirdAction" method="{1}">
    <result>/third.jsp</result>
</action>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值