Struts-接收表单的值

接收表单的值:

乱码问题:<constant name="struts.i18n.encoding" value="GBK"></constant>

A:属性设值的方式
(1):简单:在Action设置属性,生成SET与GET方法。

参数的名称与属性的名称一致。


(2):Action中设置Bean的属性

Bean必须生成SET、GET方法。Bean不可以实例化。
Bean中必须要有一个默认的空参数的构造方法。

参数的名称 = bean的名称.bean中属性的名称。


B:模型驱动的方式。

让Action实现ModelDriver的接口。实现getModel方法。
Bean必须提前实例化。

要求:参数的名称与模型中属性的名称一样。

 

C:Servlet的原生方式
HttpServlet request = SErvletActionContext.getRequest()的方法可以获取到

Servlet的HttpServletRequest对象。

 

响应数据:

普通的控件:JSTL+EL表达式

Struts控件:Struts标签 + ONGL表达式

Struts控件不支持EL表达式。


1:在Struts中获取Servlet的原生对象

通过ServletActionContext的静态方法来获取相应的对象。

HttpServletRequest

HttpServletResponse

HttpSession

ServletContext

ServletConfig

PrintWriter


2:在Struts中如何操作作用域


Request作用域

Session作用域

ServletContext作用域

 

A:使用原生的对象操作作用域

B:使用Struts封装的对象

Request作用域:对应ValueStack

Session作用域:对应Map

servletContext作用哉:对应Map


3:声明式异常:用于将Action中的异常抛出到JSp页面中。

局部的声明式异常

<action name="exceptionAction"
  class="com.action.ExceptionAction">
  <exception-mapping result="fileNotFound"
    exception="java.io.FileNotFoundException">
  </exception-mapping>
  <exception-mapping result="ioException"
    exception="java.io.IOException">
  </exception-mapping>
  <exception-mapping result="indexOfBound" exception="ArrayIndexOutOfBoundsException">
  </
exception-mapping>   <result name="fileNotFound">/error_fileNotFound.jsp</result>   <result name="ioException">/error_ioException.jsp</result>   <result name="indexOfBound">/error_indexOfBound.jsp</result> </action>

 


全局的声明式异常

<global-results>
<result name="fileNotFound">/error_fileNotFound.jsp</result>
<result name="ioException">/error_ioException.jsp</result>
<result name="indexOfBound">/error_indexOfBound.jsp</result>
</global-results>

<global-exception-mappings>
<exception-mapping result="fileNotFound"
exception="java.io.FileNotFoundException">
</exception-mapping>
<exception-mapping result="ioException"
exception="java.io.IOException">
</exception-mapping>
<exception-mapping result="indexOfBound"
exception="ArrayIndexOutOfBoundsException">
</exception-mapping>

</global-exception-mappings>

 

配置为全局后,所有的Action的异常都可以调用声明式异常对应的页面。

posted on 2015-07-17 21:53  Arvin-9  阅读(195)  评论(0编辑  收藏  举报

导航