Struct 基本原理
来源:互联网 发布:js实现类是什么 编辑:程序博客网 时间:2024/06/06 19:52
Struct标签与国际化
Struts标签
Struts提供了五个标签库,即:HTML、Bean、Logic、Template和Nested。
HTML 标签: 用来创建能够和Struts 框架和其他相应的HTML 标签交互的HTML 输入表单
html:text ; html:errors
html:errors:可以用来国际化,或者说规范化!!!在form-bean的validate方法中返回ActionErrors.
html:messages 这个是在Action中ActionForward类型的返回类型中添加的messages,回到jsp页面,需要用html:messages标签来提取!
这里的普通消息一次性全部取出,就没有对于普通消息的一个名字了…
把普通消息取出来,复制为mess别名。
把这个普通消息全部打印出来。
这里需要讲解的是:把消息当做
Bean 标签: 在访问JavaBeans 及其属性,以及定义一个新的bean 时使用
Logic 标签: 管理条件产生的输出和对象集产生的循环
Template 标签: 随着Tiles框架包的出现,此标记已开始减少使用
Nested 标签: 增强对其他的Struts 标签的嵌套使用的能力
对待这些标签,现在大多数用的都是jsp的标签了,所以需要再好好考虑一下要不要用,毕竟是很古老的东西!
需要了解 Bean:message这样一个bean包里的标签message.它是一个用来实现对国际化的支持的一个标签,配合java.util数据包中的定义的
Locale和 ResourceBundle类来完成这个任务,用java.text.MessageFormat类配置消息的格式。
首先要指定资源文件的名称,资源文件里有键值对,文件存储的位置要座位初始化参数,传递给ActionServlet.
实现国际化的规定: en, cn等,这些文件应包含相同的键名,但是关键字的值是用特定的语言实现的。
然后,ActionServlet的区域初始化参数必须与一个true值一起传递,这样ActionServlet就会在用户会话中的Action.LOCALE_KEY
关键字下存储一个en或者cn,可以运行一个国际化的web站点, 所设定的内容。可以根据用户计算机上设定的区域自动以相应的语言显示。
在资源文件中的定义: info.mykey = The numbers entered are {0},{1},{2},{3}标记的使用: <bean: message key = "info.mykey" arg0 = "5",arg1 = "2",arg2 = "45",arg3 = "25">jsp显示的页面是: The numbers entered are 5 2 45 25<bean:message>标签用于输出Resource Bundle中的一条消息.<bean:message>标签的bundle属性指定Resource Bundle,它和Struts配置文件的<message:resource>元素的可以属性相匹配.如果没有设置bundle属性,就采用默认的Resource Bundle。 Beantaglibs应用的Struts配置文件(Struts-config.xml)中配置了两个Resource Bundle: <message-resources parameter=”ApplicationResource” /> <message-resources parameter=”SpecialResouces” key=”special”/> 第一个Resource bundle 没有指定key属性,因此默认的Resource Bundle,它的资源文件ApplicationResource.properties,在这个文件中定义一条消息: .hello=Hello,{0} 第二个Resource bundle指定key属性为”special”,它的资源文件为SpecialResources.properties,这个文件中定义了一条消息: .hello=Hello,everyone! 在<bean:message>标签中指定消息key有三种方式: 1.<bean:message>标签的key属性直接指定消息key,例如: <bean:message bundle=”special” key=”hello”/> 2.<bean:message>标签的name属性指定一个可以转化为字符串的变量,这个变量的字符串值为消息key,例如: <% //这个hello要对应到资源文件中的hello Request.setAttribute(“StringBean”,”hello”); %> <bean:message bundle=”special” name=”stringBean”/> 3.同时指定<bean:message>标签的name属性和property属性.name属性指定一个JavaBean,property属性指定JavaBean的一个属性,这个JavaBean的属性的值就是消息的key,例如 <% SomeBean bean = new SomeBean(); Bean.setName(“hello”); Request.setAttribute(“someBean”,bean); %> <bean:message bundle=”special” name=”someBean” property=”name”/> 上面定义一个SomeBean类型的someBean变量,它的name属性为”hello”,因此消息的key为”hello” 对于带参数的复合消息,可以使用Bean:message标签中的arg0,arg1等属性来设置参数值 <bean:message key=”hello” arg0=”Linda”/> 资源文件ApplicationResources.properties # Resources for parameter 'org.ybystruts.ApplicationResources' # Project P/TagDemo welcome = <h1>欢迎光临!!!!!!</h1> welcome1 = <h1>欢迎 {0} 光临!!!!!!</h1> JSP文件: <%@ page language="java" contentType="text/html;charset=gb2312"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <html:html lang="true"> <head> <title>beanWrite.jsp</title> </head> <body> <!-- 通过bean:message的key能够找到属性文件{applicatinResource.properties}中的内容 --> <bean:message key="welcome"/> <!--bean:message标签中提供一个占位功能, 在输出的文件中占着一位, 这一位的数据等待标签填写 --> <bean:message key="welcome1" arg0="yby"/> <!-- <bean:message>标签的name属性指定一个可以转化为字符串的变量, 这个变量的字符串值为消息key --> <% request.setAttribute("testBean","welcome"); %> <bean:message name="testBean"/> <% SimpleBean bean = new SimpleBean(); bean.setName("welcome"); request.setAttribute("someBean",bean); %> <bean:message name="someBean" property="name"/> </body> </html:html>
http://shift8.iteye.com/blog/1319524
看了这一个很有启发!觉得不错!
bean:message标签用来从指定的locale中取回国际化的消息并输出,在这个过程中我们还可以传递五个以内的参数。message key可以通过key直接指定,也可以通过name和property间接的指定。bean:message标签有两种指定message key的方式,一是通过key属性直接指定;二是通过name和property属性间接的指定,其中message key是在message resources文件中定义的。我们可以在struts-config.xml文件中使用来设置message resources文件。
一:标签知识
/**
标签用于输出Resource Bundle中的一条消息.标签的bundle属性指定Resource Bundle,它和Struts配置文件的元素的可以属性相匹配.如果没有设置bundle属性,就采用默认的Resource Bundle。
Beantaglibs应用的Struts配置文件(Struts-config.xml)中配置了两个Resource Bundle:
<message-resources parameter=”ApplicationResource” /> <message-resources parameter=”SpecialResouces” key=”special”/>
第一个Resource bundle 没有指定key属性,因此默认的Resource Bundle,它的资源文件ApplicationResource.properties,在这个文件中定义一条消息:
.hello=Hello,{0}
第二个Resource bundle指定key属性为”special”,它的资源文件为SpecialResources.properties,这个文件中定义了一条消息:
.hello=Hello,everyone!
在标签中指定消息key有三种方式:
1.标签的key属性直接指定消息key,例如:
2.标签的name属性指定一个可以转化为字符串的变量,这个变量的字符串值为消息key,例如:
<% //这个hello要对应到资源文件中的hello Request.setAttribute(“StringBean”,”hello”);%><bean:message bundle=”special” name=”stringBean”/>
3.同时指定标签的name属性和property属性.name属性指定一个JavaBean,property属性指定JavaBean的一个属性,这个JavaBean的属性的值就是消息的key,例如
<% SomeBean bean = new SomeBean(); Bean.setName(“hello”); Request.setAttribute(“someBean”,bean);%><bean:message bundle=”special” name=”someBean” property=”name”/>
上面定义一个SomeBean类型的someBean变量,它的name属性为”hello”,因此消息的key为”hello”
对于带参数的复合消息,可以使用Bean:message标签中的arg0,arg1等属性来设置参数值
资源文件ApplicationResources.properties # Resources for parameter 'org.ybystruts.ApplicationResources'# Project P/TagDemowelcome = <h1>欢迎光临!!!!!!</h1>welcome1 = <h1>欢迎 {0} 光临!!!!!!</h1>JSP文件: <%@ page language="java" contentType="text/html;charset=gb2312"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %><%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %><%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %><%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %><html:html lang="true"> <head> <title>beanWrite.jsp</title> </head> <body> <!-- 通过bean:message的key能够找到属性文件{applicatinResource.properties}中的内容 --> <bean:message key="welcome"/> <!--bean:message标签中提供一个占位功能, 在输出的文件中占着一位, 这一位的数据等待标签填写 --> <bean:message key="welcome1" arg0="yby"/> <!-- <bean:message>标签的name属性指定一个可以转化为字符串的变量, 这个变量的字符串值为消息key --> <% request.setAttribute("testBean","welcome"); %> <bean:message name="testBean"/> <% SimpleBean bean = new SimpleBean(); bean.setName("welcome"); request.setAttribute("someBean",bean); %> <bean:message name="someBean" property="name"/> </body></html:html>*/
二:国际化实例介绍
为了介绍该标签我使用了三个message resources文件,三个文件的名字分别为Resources.properties、Resources_en.properties和Resources_zh.properties。在struts-config.xml文件中的设置(这里不用设置三个,struts会依据locale自动找到对应的文件)如下:
<message-resources parameter="Resources" />三个message resources文件中定义的message key为:<!-- Resources.properties -->resource=Resources.properties.from=Resources.properties.<!-- Resources_en.properties -->from=Resources_en.properties.<!-- Resources_zh.properties from=Resources_zh.properties.
下面的代码片段示例了bean:message标签的用法:
<bean:message key="from"/><br/><bean:message key="resource"/><br/><html:link action="/locale?language=en">English</html:link><html:link action="/locale?language=zh">Chinese</html:link>
上面的代码中含有改变locale的两个html:link标签,要使它们工作您的struts-config.xml文件中必须含有下面定义的form和action:
<form-bean name="localeForm" type="org.apache.struts.action.DynaActionForm"><form-property name="language" type="java.lang.String" /><form-property name="country" type="java.lang.String" /><!--action成功后要跳到那里--><form-property name="page" type="java.lang.String" initial="/message.jsp"/></form-bean><action path="/locale" type="org.apache.struts.actions.LocaleAction"name="localeForm" scope="request"></action>
在不同的locale下我们得到了如下的两个结果:
在locale为zh时的结果:Resources_zh.properties.Resources.properties.在locale为en时的结果: Resources_en.properties.Resources.properties.让我们来看一下在locale为zh时如何得到的是上面的结果。因为locale为zh所以<bean:message key="from"/><br/>先找Resources_zh.properties这个文件从中得到form键的值。而<bean:messagekey="resource"/><br/>也会先找Resources_zh.properties这个文件但这次没有找到resource键,这时Struts会到Resources.properties这个文件中找,很幸运这里找到了。如果还没有找到,或messageresource文件不存在就会抛出异常。当locale为en时类似,可以自己试试。
======================================================华丽的分割线
用struct了解了程序运行的流程,其中主要重要的是,运行机制。
一个jsp上的form表单可以直接跳到另一个页面, action=”save.jsp”,只要用
或者action=”save.do”,则 ActionServlet就会加载一个叫SaveAction(继承Action的子类),同时,表单需要一个formBean,这个formBean同样是继承一个基类做出来的,处理完以后的结果通过struct-config.xml找到需要加载的其他页面!
其中可以有数据库的参与!服务service的参与等!
Struts组件
struts有5个主要的组件
Action组件 ActionForm组件,ActionForward, ActionServlet , ActionMapping组件
状态改变的数据ActionForm
动态表单DynaActionForm 是在配置文件中配置所需的虚拟ActionForm,而不必再创建一个具体的ActionForm类,目的是为了减少ActionForm的数目。
用法如下:
<form-beans> <form-bean name="loginForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="username" type="java.lang.String" /> <form-property name="userpass" type="java.lang.String" /> </form-bean> </form-beans> ```在具体的一个Action中,可以这样获取: ```DynaActionForm loginform = (DynaActionForm) actionForm;String username = (String)loginform.get("username");String userpass = (String)loginform.get("userpass");String actionpath = "";if (!username.equals("") ) { HttpSession session = servletRequest.getSession(); session.setAttribute("name", username); actionpath = "success";} else { actionpath = "error";}
用户指向或视图选择ActinForward
代表一个web资源的逻辑抽象。全局转发和局部转发,全局转发对全局有效咯。
状态改变事件ActionMapping
控制器,接受用户请求或状态改变,以及发出视图选择ActionServlet
控制器的一部分,用于模型交互,执行状态改变或状态查询,以及告诉ActionServlet下一个选择的视图
继承自ActionConfig类,用来保存标记的所有属性的值
ActionServlet是系统维护的,你不能操作,它主要是根据传入的action路径在ActionMapping中查找相应的类,并调用它。
可以自定义RequestProcessor类,从此来解决中文乱码问题,虽然在登陆页和结果页都设定了字符编码,gbk,但是浏览器默认使用UTF-8编码方式来发送请求,所以会出现乱码。这里可以设置request.setCharacterEncoding(“gb2312”)对请求进行统一编码,实现中文的正常显示。
自定义一个RequestProcessor.java类
import java.io.UnsupportedEncodingException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.RequestProcessor;public class EncodingHandler extends RequestProcessor { public boolean processPreprocess(HttpServletRequest servletRequest, HttpServletResponse serveltResponse) { try { servletRequest.setCharacterEncoding("gb2312"); } catch (UnsupportedEncodingException ex) { } //要使这个方法被调用,必须要设置这里的返回值为true return true; }}
修改配置文件
Action是控制器,主要是从ActionForm中接收页面传进来的数据,然后进行逻辑处理。
是用户请求和业务逻辑之间的桥梁,每一个Action充当客户的一项业务代理。
Action类中具体处理业务的是execute();它的参数是4个,分别为
ActionMapping, ActionForm,HttpServletRequest , HttpServletResponse
就这四个。
Struts提供了一些现成的Action类,在应用中,使用这些类可以大大节省开发时间,这些类有:
org.apache.struts.action.Action;
org.apache.struts.action.ForwardAction; 实际上用的比较少
org.apache.struts.action.IncludeAction; 包含页面进去
org.apache.struts.action.DispatchAction; 实际编程用的非常多的东西…
DispatchAction
<form action="student.do" method="post"> <table width="402" border="1" cellpadding="1" cellspacing="0"> <tr> <td width="115" align="center"> 密码: </td> <td width="280"> <input type="text" name="password" /> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="Submit" value="删除" /> <input type="reset" name="reset" value="重置" /> <input type="hidden" name="method" value="delete" /> </td> </tr> </table> </form>
这里有个字段,name = “method”, value = “delete”,通过提交这样一个字段,同时在struts-config.xml中,有如下配置
<!-- 在该Action配置中,增加了parameter属性,该属性用于指定参数名,即Struts将根据该参数的值调用Action中对应的方法。 --> <action path="/student" name="stuForm" parameter="method" type="org.njy.action.StudentAction"> <forward name="success" path="/success.jsp" /> </action>
从而,直接调用 StudentAction(extends DispatchAction) 中的method 对应的value方法!!!
这个功能调!!!//针对同一实体的类似功能使用DsipatchAction来处理
org.apache.struts.action.LookupDispatchAction;org.apache.struts.action.SwitchAction;
使用的不是很多,不多说了。。。
ActionForm是用来接收页面上表单中的数据。
ActionMapping是用来配置每个Action所对应的路径
ActionForward是用来实现跳转,在Action中最后一行语句通常是return mapping.forward()这里传入的值就在ActionForward中设置
下一章介绍Struts标签!
- Struct 基本原理
- 基本原理
- 基本原理
- 基本原理
- Struct
- struct
- struct
- struct
- Struct
- struct
- struct
- struct
- struct
- struct
- struct
- struct
- struct
- struct
- Extract Abends with ERROR OGG-01733 Oracle GoldenGate Capture
- java中的SecureRandom在linux中的实现
- SDWebImage的简单使用
- vlc 播放器的播放进度条的时间不刷新
- hdu 3336
- Struct 基本原理
- 斐波那契博弈(转)
- JavaVM和JNIEnv
- MyEclipse 2015优化技巧
- C++Primer第五版 5.5.1节练习
- 网易面试问题
- Android基础入门教程——7.5.4 WebView文件下载
- 网络编程
- Win32 线程知识点梳理一