---
title: "struts2完全学习文档"
author: "aniu2008"
date: "2011-08-04T20:11:16Z"
tags: []
categories: ["Web前端"]
slug: "struts2"
draft: false
lastmod: "2011-08-04T20:11:16Z"
id: 1140401
url: "https://aniu2008.iteye.com/blog/1140401"
---

struts2完全学习文档 分类: Java编程技巧 发布: bywei 浏览: 日期: 2011年3月30日 把本文分享给您的朋友，获取免费教程！ 分享到： QQ空间 新浪微博 编译问题： java(jdk)编译设置：window-per\*\*-java-instelled jar java Compiler 编译设置与jdk的配置一样 Tomcat 使用的jdk与编译设置的jdk也应该是一致的 Acion的配置： 1.Struts的开发模式设置 2.Struts中web.xml的配置文件的设置：...... 3.为jar文件建立源文件doc时，按f1可以导航到相关的文档 4.为配置文件设置提示 为 xml catalog 配置相关的dtd文件。 5.运行机制：客户端-tomcat-web.xml-filter-filter class-action-result 6.Namespace: (1)'/'-&gt;/index.action (2)'/\*\*' -&gt;/\*\*/index.action (3)'/\*\*/\*\*' -&gt;/\*\*/\*\*/index.action 为对应的模块名。 namespace 不写，将默认接受所有的action result若没有写名字，将默认为'success'. extends: 从另外一个包来继承,声明一个主要配置,然后所有的都继承该包. 7.action返回：当struts.xml中没有调用方法时，默认调用execute()方法。 struts.xml中的action中没有class是默认调用ActionSupport的execute(); Action中有默认常量：SUCCESS ERROR INPUT等。 可以继承implements Action / extends ActionSupport 8.Action方法调用： (1)struts.xml中的action的属性指定 method="方法名" (2)struts.xml中的action不配置属性method,而在url中使用 ‘!方法名’（dmi动态方法调用）来动态调用。 9.通配符的配置： 使用“\*”代表所有字符，使用“{1开始的数字调用第几个\*}” 使用法则：约定优于配置(提前把文件名约定好) 10.action接收参数三种方法： (1)(常用)通过Action属性来接收参数,属性必须写getter/setter方法。属性自动转换类型。调用时：url?属性=值&属性=值 (2)(常用)通过DomainModel(域模型)接收参数，使用Model生成getter/setter,调用时：url?model.属性=值&model.属性=值 --(上)通过Dto(数据传输对象)接收参数，使用同DomainModel (3)通过ModelDriven接收参数，在Action中 implements ModelDriven,然后又在Action中new 一个Model（必须自己new Model()）,最后再继承 getModel()方法。 11.中文问题解决： struts.xml中使用i18n配置， --i18n internationalization 配置文件参考：struts-core.jar/org.apache.struts2/default.properties 12.简单数据校验 在action中配置一个逻辑 然后调用 this.addFieldError("属性","错误信息");在Struts配置错误返回页面。 页面使用struts标签 打印错误信息： 错误信息保存在一个数组中。 显示调试信息 13.Action访问web元素 request,session,application,HttpRequest,HttpSession (1)request=(Map)ActionContext.getContext().get("request"); session=ActionContext.getContext().getSession(); application=ActionContext.getContext().getApplication(); Action赋值： request.put("属性","值"); session.put("属性","值"); application.put("属性","值"); jsp取值： (2)(常用)Action implements RequestAware,SessionAware,ApplicationAware (这也是IOC/DI的实现),定义Map request session 和application. (3)在Action中定义 private HttpServletRequest request; private HttpSession session; private ServletContext; Action取值： request= ServletActionContext.getRequest(); session=request.getSession(); application=session.getServletContext(); (4)Action implements ServletRequestAware private HttpServletRequest request; @override setServletRequest(\*\*){request=request;session=request.getSession();} 14.模块包含 15.默认Action Result的配置： 1.类型type: (1)dispatcher 服务器端跳转到视图 (2)redirect 客户端跳转 (3)chain 服务器端跳转到Action和视图，Action前面不要加"/",有参数param(actionName,namespace)两个属性(跳转到其他包); (4)RedirectAction跳转到其他Action, 地址栏显示跳转后的Action地址 2.全局结果集 /other.jsp 3.向结果集传递参数 (1)url?type=typeValue (2)Action中声名该属性type (3)result中取值/url?type=${type} (4)jsp页面取值:O 本文由 程序员百味 http://www.bywei.cn/blog 编写 OGNL(对象Graph导航语言)表达式和struts标签(Action中演示变量:name属性,model实体类) -------&gt;OGNL表达式 1. 2. 3. 4. 调用普通方法 5.调用普通方法 6.调用普通方法 7.struts.xml配置: 访问值棧中的静态方法 访问值棧中的静态属性 8.调用构造方法 9.访问集合: (1) 访问所有 访问index的属性集合 访问属性集合中的一个 (2) (3) / 10.过滤(投影) age=q的model, this指定拿到的当前对象 age&gt;1的开头的那个 age&gt;1的结尾的那个 11.通过盏值访问 代表盏值中的index个对象的属性name --------&gt;struts2标签 12. 从request取值: 从Action中取值: 13. 14. 15.(少用) 包含静态英文文件 16. 其中的tehme设置:(1)css样式表解决 (2)xhtml解决(-&gt;struts-core.jar/template/fielderror.ftl --&gt;struts.xml中配置:) 17. 18.struts.xml中配置: /error.jsp 3.全局异常映射 --&gt;struts.xml中配置: /error.jsp 国际化 1.Local 获取本地信息,ResourceBundle 绑定本地properties文件 建立message\_zh\_CN.properties 2.struts国际化 (1)类级别properties文件必须和对应的类名相同(Class\_zh\_CN.properties) 包级别properties文件在包下 全局properties,必须在struts.xml中配置: (2)jsp页面获取: (3)传递参数动态显示: --在properties文件中配置{0}占位符 --jsp页面获取: 动态语言切换:使用连接[中文](?request_loale=zh_CN) 控制重复提交 1.在form表单中设置method="post" 2.在表单中设置 token-令牌 struts.xml 中配置: /errot.jsp 类型转换 1.Action默认已经做了类型转换 2.日期转换: 3.其他类型的转换,如:list,set,map以及自定义的类型转换少用 本文由 程序员百味 http://www.bywei.cn/blog 编写