Struts2学习笔记

一、Struts2框架搜索Struts2常量的顺序:

      1. struts-default.xml 
      2. struts-plugin.xml 
      3. struts.xml 
      4. struts.properties 
      5. web.xml

 

二、配置

     在web.xml文件配置Struts2过滤器

 

     1、配置包:一个包可以继承其它包。抽象包不能包含Action定义。父包应该在子包前面定义。

 

     2、配置命名空间:默认的命名空间是"",命名空间必须以“/”开头。命名空间只有一个级别。

 

     3、配置Action:

          当配置Action没有指定class属性时,系统自动使用ActionSupport类作为默认的Action处理类。

          1)、动态方法调用
                调用格式:ActionName!methodName.action
                需要设置struts.enable.DynamicMethodInvocation为true

          2)、为action元素指定method属性
                <action name="login" class="com.cjm.LoginAction" method="login">

                </action>

          3)、使用通配符
                <action name="*Action" class="com.cjm.LoginAction" method="{1}">
                <action name="*_*" class="com.cjm.{1}" method="{2}">
                <action name="*">
                      <result>/{1}.jsp</result>
                </action>

          4)、默认Action
                <default-action-ref name="defaultAction">

 

     4、配置结果:
          1)、Struts2默认的结果类型是dispatcher,结果默认的name属性值为success
                <result name="success" type="dispatcher">
                      <param name="location">/login.jsp</param>
                      <param name="charSet">GBK</param>
                </result>

          2)、redirect-action结果类型:
                <result name="success" type="dispatcher">
                      <param name="actionName">show</param>
                      <param name="namespace">/sales</param>
                </result>

          3)、在结果里使用OGNL表达式
                格式:${属性名},属性名就是对应Action实例里的属性。
                <result>/${currentSkill.name}.jsp</result>

          4)、全局结果
                <global-results>
                      <result name="success">/target.jsp</result>
                </global-results>

 

     5、配置异常:
          1)、全局异常
                <global-exception-mappings>
                      <exception-mapping exception="java.sql.SQLException" result="sql"/>
                      <exception-mapping exception="java.sql.RuntimeException" result="runtime"/>
                </global-exception-mappings>

          2)、输出异常
                <s:property value="exception"/>   输出异常对象本身
                <s:property value="exception.message"/>   
                <s:property value="exceptionStack"/>   输出异常堆栈信息

 

     6、结果类型的用法

          1)、stream

Java代码
  1. public class HelloWorldAction extends BaseAction {   
  2.     private InputStream inputStream;   
  3.   
  4.     public InputStream getInputStream() {   
  5.         return inputStream;   
  6.     }   
  7.        
  8.     public String stream()throws Exception{   
  9.         inputStream = new StringBufferInputStream("test result type of stream!");   
  10.         return "stream";   
  11.     }   
  12. }  
public class HelloWorldAction extends BaseAction {
	private InputStream inputStream;

	public InputStream getInputStream() {
		return inputStream;
	}
	
	public String stream()throws Exception{
		inputStream = new StringBufferInputStream("test result type of stream!");
		return "stream";
	}
}

 

Xml代码
  1. <package name="test" extends="struts-default" namespace="/test">  
  2.     <action name="helloWorld" class="com.cjm.web.action.HelloWorldAction">  
  3.         <result name="stream" type="stream">  
  4.             <param name="contentType">text/html</param>  
  5.             <param name="inputName">inputStream</param>  
  6.         </result>  
  7.     </action>  
  8. </package>  
<package name="test" extends="struts-default" namespace="/test">
	<action name="helloWorld" class="com.cjm.web.action.HelloWorldAction">
		<result name="stream" type="stream">
			<param name="contentType">text/html</param>
			<param name="inputName">inputStream</param>
		</result>
	</action>
</package>

 

 

三、页面取值

      取action里bean属性的值:
            <s:property value="message"/>

 

      取request的属性值:
            ServletActionContext.getRequest().setAttribute("n1", obj);
            <s:property value="#request.n1"/>

 

      取session里的值:
            ActionContext.getContext().getSession().put("n2", obj);
            <s:property value="#session.n2"/>

 

      取application里的值:
            ActionContext.getContext().getApplication().put("n3", obj);
            <s:property value="#application.n3"/>

 

      取ActionContext里的值:
            ActionContext.getContext().put("n4", obj);
            <s:property value="#attr.n4"/>

 

      取URL传过来的参数:
            http://localhost:8888/struts2/helloWorld!show.action?name=liurun
            <s:property value="#parameters.name"/>

 

 

      取在页面用<s:set/>赋的值:
            <s:set name="uid" value="'liurun'"/> ————此处的值要用单引号括起来,否则,将当变量处理
            <s:property value="#uid"/>
            <s:property value="#attr.uid"/>

 

      在控件中取值:
            <s:textfield name="name"  value="%{#parameters._KEEP_MULTI_ENTITY}"/>
            <s:textfield name="name"  value="%{#parameters.name}"/>

 

四、Struts2核心对象

          1)、ServletActionContext

                 HttpServletRequest request = ServletActionContext.getRequest();
                 HttpServletResponse response = ServletActionContext.getResponse();
                 ServletContext servletContext = ServletActionContext.getServletContext();
                 ValueStack valueStack = ServletActionContext.getValueStack(request);
                 PageContext pageContext = ServletActionContext.getPageContext();
                 ActionContext actionContext = ServletActionContext.getContext();

 

          2)、ActionContext

                 ActionContext.getContext().getActionInvocation();  —— return ActionInvocation
                 ActionContext.getContext().getApplication();  —— return Map
                 ActionContext.getContext().getSession();  —— return Map
                 ActionContext.getContext().getParameters();  —— return Map
                 ActionContext.getContext().getValueStack();  —— return ValueStack
                 ActionContext.getContext().getContainer();  —— return Container
                 ActionContext.getContext().get(key);

 

          3)、跟Servlet交互的接口

                 ParameterAware
                 ServletRequestAware
                 ServletResponseAware
                 SessionAware
                 ApplicationAware
                 CookiesAware

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值