Action
------------------------------------------------
0. 实现一个action最常用的方式:继承ActionSupport(ActionSupport实现Action接口)
1. 打开开发模式
struts.xml <constant name="struts.devMode" value="true" />
2. 打开xml输入提示 输入<提示
先在struts中lib目录中解压struts2-core-2.1.8.1.jar文件
再复制 xml 文件中 http://struts.apache.org/dtds/struts-2.0.dtd 这句
然后 window --> Preferences --> MyEclipse --> Files and Editors --> XML --> XML Catalog --> Add
Location: struts-2.0.dtd的位置
Key Type: URL
Key: http://struts.apache.org/dtds/struts-2.0.dtd
3. struts作用:把请求和视图分开
4. 更改JSP默认编码方式
window --> Preferences --> 输入JSP 在JSP中Encoding:改为Chinese,National Standaed
5. JSP页面些链接用绝对路径
页面开头要加上:
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
方法一:
然后下面链接写:
<%=basePath %>xxx.jsp
不加<%=basePath %> 直接写/xxx.jsp则是相对于服务器的根目录的绝对路径,即http://localhost:8080/ 而不是当前项目的
直接写 xxx.jsp则是相对路径
方法二:
在head中加标签 <base href="<%=basePath%>"> 则页面中所有链接前面都自动加上basePath 所以直接写 xxx.jsp 就是当前的绝对路径
6. 动态DMI方法调用 (推荐)
action中不指定方法,链接中用 /namespace名称/action名称!方法名
7. 通配符
多个action都可匹配,则匹配最精确的没*号的
若action中含有*号,则匹配最前面的
约定由于配置
8. action传值接收参数
方法一:
链接中挂参, ?name=xxx&pass=123
直接用类中getset方法接受
方法二:(常用)
预模型 类中是实体类user 没有属性
用 ?user.name=xxx&user.pass=123接受
两个类中均有getset方法
预模型中接受的参数必须和类中属性参数个数名称一致
方法三:
实现ModelDriven<User>接口
private User user=new User();
实现方法public User getModel{return user};
则可以用预模型,但链接中只写name=xxx&pass=123
传值有中文,尽量不要用get方法,全用post方法
9. 中文乱码
struts.xml配置 <constant name="struts.i18n.encoding" value="GBK" />
<!-- i18n:internationlization -->
10. 一表单多按钮提交
<form name="f" action="" method="post">
用户名:<input type="text" name="name"/>
密码:<input type="text" name="password"/>
<br />
<input type="button" value="submit1" οnclick="javascript:document.f.action='login/login1';document.f.submit();" />
<input type="button" value="submit2" οnclick="javascript:document.f.action='login/login2';document.f.submit();" />
<input type="button" value="submit3" οnclick="javascript:document.f.action='login/login3';document.f.submit();" />
<input type="button" value="submit4" οnclick="javascript:document.f.action='login/login4';document.f.submit();" />
</form>
11. 在struts.xml中部署其他xml文件
<include file="xxx.xml" />
12. 设置默认action,系统找不到请求的action 则转到此action
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index"></default-action-ref>
<action name="index">
<result>/default.jsp</result>
</action>
</package>
*************************************************
*****************************************************
Result
--------------------------------------------------------
13. result类型(只用前四种)
dispatcher(默认):运用服务器跳转,即jspforward,只能跳转到一个页面,不能是action,地址栏显示action名
redirect:客户端跳转,只能跳转到页面,不能到action,地址栏显示页面名称
chain:forward到另一个action,配置中要跳转到action名前不加/,地址按显示action名
redirectAction:客户端跳转到一个action,地址栏显示页面名称
freemarker:
httpheader:发一个http的头信息过去
stream:下载,result类型
velocity:和freemarker类似的一个模板框架
xslt:和xml相关的修饰
plaintext:把页面源码显示出来,带<%%>之类的
titles:将页面分为几块,每块都可动态指定
14. 全局result
<package name="user" namespace="/user" extends="struts-default">
<global-results>
<result name="mainpage">/main.jsp</result>
</global-results>
</package>
其他包中也要用此result,则需继承此包即可
<package name="admin" namespace="/admin" extends="user">
</package>
15. 动态获取result
class中用属性r接受结果
<action name="xxx" class="xxx">
<result>${r}</result>
</action>
*****************************************************
*****************************************************
OGNL
--------------------------------------------------
16. 允许ognl访问静态方法访问
struts.xml中配置:
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
*****************************************************
*****************************************************
Tag标签
--------------------------------------------------
17. include标签尽量少用,若包含页面中含有中文字符,可能会出问题,用jsp的标签
include中value类型为string
<s:set var="incPage" value="'/_include.html'" />
<s:include value="%{#incPage}" /> 此处用%将其转换为OGNL表达式,否则只是字符串
18. $ # % 的区别
$:用于i18n和struts配置文件
#:取得actioncontext的值
%:将原本的文本属性解析为OGNL,对于本来就是OGNL的属性不起作用
19. iterator遍历标签
可以遍历的有:
collection(包括list set stack等) map enumeration iterator(接口) array
************************************************************
************************************************************
规范
-------------------------------------------------------------
0. 原则:能简单就不要复杂
1. 表命名 _Model名
_ShoppingItem 前面加个标记,以防关键字, 最好驼峰标记
2. 字段命名
保持和属性名一致(尽量不要取名和数据库命名冲突)
3. 库名
项目名
************************************************************
************************************************************
ext3.0
-------------------------------------------------------------
1. 新建一个javaweb项目
2. 将解压后的ext3.0文件夹内所有文件全部拷贝到WebRoot目录下
3. 部署到tomcat上
4. 框架右边的html 用 iframe 将其src映射到要访问的链接或action(此方法不推荐,合理写法应用ajax)
html:'<iframe width=100% height=100% src='+btn.id+' />',
用ext则将项目中所有编码均改为utf-8 因ext中用此编码格式
**************************************************************
****************************************************************
struts.xml配置文件bug
<default-action-ref name="xxx"/> 不能和 <action name="*_*" ...></actionn> 一起用 可以用 *--*
**************************************************************
****************************************************************
struts 异常处理
声明式异常
在方法中throws Exception
<action>
<exception-mapping result="error" exception="java.sql.SQLException" />
<result name="error">/error.jsp</result>
</action>
方法二:
<package name="bbs2009_default" extends="struts-default">
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
</package>
<action>
<result name="error">/error.jsp</result>
</action>
两个result只能存在一个
**************************************************************
****************************************************************
i18n国际化
在src中建文件
app_en_US.properties 写入:welcome.msg=hello,sir
app_zh_CN.properties 写入:welcome.msg=nihaoya
方法中:
ResourceBundle res=ResourceBundle.getBundle("app",Locale.US);
System.out.println(res.getString("welcome.msg"));
-------------------
将编码转换为ascii码
cmd打开命令提示符
D:\soft\Java\jdk1.6.0_18\bin\native2ascii
这个命令可以将字符编码转换为ascii
因此,可以这样转换
D:\soft\Java\jdk1.6.0_18\bin\native2ascii app_zh_CN_temp.properties app_zh_CN.propertoes
就可以将字符转化为ascii格式
或者 将properties文件右击用PropertiesEditor格式打开 就可以写中文了
这种方法需要有插件支持 jp.gr.java_conf.ussiy.app.propedit_5.3.3.zip
-------------------------
Action级别的国际化文件
LoginAction.java 继承ActionSupport
ActionSupport_en_US.properties 写入:login.username=username
ActionSupport_zh_CN.properties 写入:login.username=用户名
与LoginAction.java同级别且名字相同
页面中读取国际化文件
<s:property value="getText('login.username')" />
调用方法只能调用action中的方法
包级别的国际化文件
package_en_US.properties 包级别的 只对当前包起作用
全局级别的国际化文件
放在根目录下的对全局都起作用
如名字为bbs2009_en_US.properties,需在struts.xml中配置 <constant name="struts.custom.i18n.resources" value="bbs2009" />
若3中都存在,哪个起作用???
-----------------------------
处理参数
properties文件中 welcome.msg=welcome:{0} 0是占位符
页面中
<s:text name="welcome.msg">
<s:param value="username"/>
</text>
----------------------------------------
页面中英文切换链接
1. 新建action LangAction.java extends ActionSupport
public String execute(){
return super.execute();
}
2. struts.xml文件
<action name="lang" class="com.xxx.LangAction">
<result name="input">/admin/Login_input.jsp</result>
</action>
3. 页面中
<a href="admin/lang?request_local=en_US">en</a>
<a href="admin/lang?request_local=zh_CN">cn</a>