Struts2学习笔记.docx
《Struts2学习笔记.docx》由会员分享,可在线阅读,更多相关《Struts2学习笔记.docx(25页珍藏版)》请在冰点文库上搜索。
Struts2学习笔记
Struts21
重点:
搭Struts环境1
一个Action跳到另一个action4
Action中实现多个方法5
多个配置文件6
全局结果7
加载顺序7
常用标签7
i18n(国际化)8
重点:
拦截器10
重点:
验证12
上传:
14
中文问题14
Struts2
传统的MVC:
JSP+Servlet+JavaBean
Struts实现的重点在Controller,包括ActionServlet,RequestProcessor和我们定制的Action
Struts没有涉及到Model,更多在视图和控制层
Struts1中靠继承实现,容易引起类爆炸,单元测试难
Struts2(Struts和webwork)
重点:
搭Struts环境
1)将Struts2的jar包复制到项目的lib下
2)创建struts.xml文件(创建在src目录下)
将以下代码贴入:
(文件头)
DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http:
//struts.apache.org/dtds/struts-2.0.dtd">
3)配置web.xml文件(是以过滤器的形式存在)--加载Struts是调用web.xml中写的过滤器
贴入代码:
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
实例1.
1)login.jsp
--和xml中Action的名字一致-->
用户名:
密码:
2)LoginAction
publicclassLoginAction{
privateStringusername;//接收前台表单的内容,名字和前台表单相同----IOC(翻转注入)
privateStringpwd;
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPwd(){
returnpwd;
}
publicvoidsetPwd(Stringpwd){
this.pwd=pwd;
}
//Struts中Action默认的执行方法
(1)
publicStringexecute(){
if("wyy".equals(getUsername())&&"123456".equals(getPwd())){
return"success";
}else{
return"error";
}
}
(2)
publicStringexecute(){
if("wyy".equals(getUsername())&&"123456".equals(getPwd())){
ActionContext.getContext().getSession().put("user",getUsername());//获得session
return"success";
}else{
return"error";
}
}
3)struts.xml
--extends="struts-default"为默认,name可以随便起-->
--name随便起,class为文件路径-->
/index.jsp--name和返回的字符串一致-->
/error.jsp
3)index.jsp
欢迎登陆:
${user}
注意:
1)Struts的跳转默认的为服务器端的跳转
2)改为客户端跳转,在中加入type=”redirect”
3)Action跳另一个Action,type=”redirectAction”
一个Action跳到另一个action
实例2):
LoginAction
publicStringexecute(){
if("wyy".equals(getUsername())&&"123456".equals(getPwd())){
ActionContext.getContext().getSession().put("user",getUsername());//获得session
return"success";
}else{
return"error";
}
}
1)UserAction
publicclassUserAction{
publicStringexecute(){
Stringname=(String)ActionContext.getContext().getSession().get("user");
if(!
name.equals("")){
if("wyy".equals(name)){
ActionContext.getContext().getSession().put("grade","好孩子");
}else{
ActionContext.getContext().getSession().put("grade","坏孩子");
}
return"success";
}
return"error";
}
2)struts.xml
--extends="struts-default"为默认,name可以随便起-->
--name随便起,class为文件路径-->
userAction--跳转到另一个action,注意名字要一致(和下面的action的名字一致)-->
/error.jsp
/index.jsp
/error.jsp
4)index.jsp
欢迎登陆:
${user}是${grade}
Action中实现多个方法
实例3:
(1)action中实现多个方法,返回值是String
(2)修改struts.xml文件对应的action配置
1)LoginAction
publicStringlogin(){
if("wyy".equals(getUsername())&&"123456".equals(getPwd())){
ActionContext.getContext().getSession().put("user",getUsername());
return"success";
}
return"error";
}
publicStringregist(){
Stringname=(String)ActionContext.getContext().getSession().get("user");
if(name.equals("")){
ActionContext.getContext().getSession().put("msg","注册成功");
return"reg_success";
}else{
ActionContext.getContext().getSession().put("msg","注册失败");
return"reg_error";
}
}
2)struts.xml(_*是通配符,method=’${1}’代表有一个通配符)
userAction/error.jsp
/success.jsp
/error.jsp
/index.jsp
/error.jsp
3)login.jsp
4)registe.jsp
多个配置文件
Struct.xml包括若干个子配置文件,用
利用名称空间(namespace)
实例(两个子配置文件)
1)struts-login.xml
/index.jsp
/error.jsp
2)struts-reg.xml
/index.jsp
/error.jsp
3)RegistAction
publicclassRegistAction{
privateStringusername;
privateStringpwd;
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPwd(){
returnpwd;
}
publicvoidsetPwd(Stringpwd){
this.pwd=pwd;
}
publicStringexecute(){
Stringname=(String)ActionContext.getContext().getSession().get("user");
System.out.println(name);
if(name.equals("")){
ActionContext.getContext().getSession().put("msg","注册成功");
return"reg_success";
}else{
ActionContext.getContext().getSession().put("msg","注册失败");
return"reg_error";
}
}
}
5)login.jsp
6)register.jsp
全局结果
加载顺序
ØStruts-default.xml
ØStruts-plugin.xml
ØStruts.xml
ØStruts.properties
ØWeb.xml
自带的五种字符串
Action.SUCCESS
Action.ERROR
Action.INPUT
Action.LOGIN
Action.NONE
常用标签
在jsp的顶部加入
<%@taglibprefix=”s”uri=”/struts-tags”%>
1)if-else
iftest="#msg!
=''">${msg}
if>
else>
出错了
else>
绿色字体为OGNL表达式(有三种#,%,$)
全写为#session.msg!
=’’
2)迭代标签(s:
iterator)输出(property)序号(propertyvalue="#st.getIndex()+1"/>)
iteratorvalue="#session.content"id="people"status="st">
propertyvalue="#st.getIndex()+1"/>
propertyvalue="people"/>
iterator>
如果是vo,people.属性;people.方法
RegistAction
Listlist=newArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
ActionContext.getContext().getSession().put("content",list);
3)URL标签(Action的路径)Param加参数
urlaction="reg/registAction"id="reg">
paramname="id"value="10"/>
url>
ahref="%{#reg}">注册
a>
注意:
百分号的作用是在标志的属性为字符串时
3)令牌环:
避免Action的重复提交
token/>放入form表单
防止重复提交的方法
①服务器端跳转改为客户端跳转
②令牌环---只针对action
③js
i18n(国际化)—改变的是静态内容
1)创建资源文件—src目录下
例:
定义resource文件(名字随便起)
resource_en_US.properties(蓝色为固定,前面为语言,后面是国家)
resource_zh_CN.properties
实例:
1)LanguageAction
publicclassLanguageAction{
privateStringlocale;
publicvoidsetLocale(Stringlocale){
this.locale=locale;
}
publicStringgetLocale(){
returnlocale;
}
publicStringexecute(){
Localelocale=ActionContext.getContext().getLocale();//获得系统的本地默认语言
Stringlanguage=getLocale();
if(language!
=null&&language.length()>0){
if(language.equals("en")){
locale=newLocale(language,"US");
}else{
locale=newLocale(language,"CN");
}
}
ActionContext.getContext().setLocale(locale);
return"success";
}
}
2)struts.xml
/login.jsp
3)login.jsp
textname="lab.user">
text>
textname="lab.pwd">
text>
textname="lab.button">
text>">
重点:
拦截器
必须在struts2框架中使用,只能拦到Action,是AOP(面向切面---面向同一种类)的一种实现,是可插拔的
面向对象(单个对象)
①实现权限的控制
②实现日志的控制
实例1)实现权限的控制
1)LoginInterceptor
publicclassLoginInterceptorextendsAbstractInterceptor{
@Override
publicStringintercept(ActionInvocationinvocation)throwsException{
//TODOAuto-generatedmethodstub
Stringname=null;
//name=(String)ActionContext.getContext().getSession().get("user");
name="wyy";
if(name!
=null&&name.equals("wyy")){
returninvocation.invoke();
}else{
ActionContext.getContext().getSession().put("msg","权限不足");
returnAction.LOGIN;
}
}
}
2)struts-login.xml
--调用缺省的拦截器,否则自己写的不起作用-