当前位置: 首页 > news >正文

上海浦东建设管理有限公司网站凡客诚品图片

上海浦东建设管理有限公司网站,凡客诚品图片,长沙百度关键词优化,网站用户访问统计注意每次修改配置文件后必须项目重启 Structs2structs1xwork Struct2框架预先实现了一些功能#xff1a; 1.请求数据的封装#xff1b;2.文件上传的功能3.对国际化功能的简化4.文件效验功能 1.开发Structs框架的步骤#xff1a; 1)引入8大jar包 commons-fileupload-1.2.2.ja… 注意每次修改配置文件后必须项目重启 Structs2structs1xwork Struct2框架预先实现了一些功能 1.请求数据的封装2.文件上传的功能3.对国际化功能的简化4.文件效验功能 1.开发Structs框架的步骤 1)引入8大jar包 commons-fileupload-1.2.2.jar   【文件上传相关包】 commons-io-2.0.1.jar struts2-core-2.3.4.1.jar           【struts2核心功能包】 xwork-core-2.3.4.1.jar           【Xwork核心包】 ognl-3.0.5.jar  【Ognl表达式功能支持表】 commons-lang3-3.1.jar          【struts对java.lang包的扩展】 freemarker-2.3.19.jar            【struts的标签模板库jar文件】 javassist-3.11.0.GA.jar           【struts对字节码的处理相关jar】 2)配置web.xml 主要配置filter Struct过滤器,StructsPrepareAndExecuteFilter核心过滤器 //引入struct核心过滤器 struct2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struct2 /* 3)开发Action 方法满足无参数放回值为Stringpublic修饰 编写普通类1.继承ActionSupport有数据效验时必须继承;2.或者实现action接口 3.什么都不写直接return ”success”再从structs.xml中找到对应的返回页面 例如 public String register() { System.out.println(register() userName); return register; } 再配置文件中配置 /index.jsp 可以使用通配符优化配置 /{1}.jsp public class HelloAction extends ActionSupport { // 处理请求 public String execute() throws Exception {} } 4)配置struct.xml /p -//Apache Software Foundation//DTD Struts Configuration 2.0//EN http://struts.apache.org/dtds/struts-2.0.dtd /index.jsp 2.访问流程 tomcat服务器启动-》读取web,xml-》读取struct2核心过滤器-》初始化过滤器-》init方法(这里分别读取了struct-default.xml核心功能初始化有拦截器等struct-plugin.xmlstruct相关插件struct.xml用户编写的xml)-》读取到struct.xml后找到action类-》读取structs.properties用户自定义配置文件会覆盖Structs.xml中的常量设置-》加载到内存中等待访问再实例化action类 3.一些配置文件详解 struct-default.xml 目录struts2-core-2.3.4.1.jar/ struts-default.xml 内容 1. bean节点指定struts在运行的时候创建的对象类型 2.指定struts-default包 【用户写的package(struts.xml)一样要继承此包 】 package struts-default 包中定义了 a. 跳转的结果类型 dispatcher 转发不指定默认为转发 redirect 重定向 redirectAction 重定向到action资源 stream (文件下载的时候用) b. 定义了所有的拦截器 定义了32个拦截器 为了拦截器引用方便可以通过定义栈的方式引用拦截器 此时如果引用了栈栈中的拦截器都会被引用! defaultStack 默认的栈其中定义默认要执行的18个拦截器 c. 默认执行的拦截器栈、默认执行的action View Code 自己的struct。xml配置 1)两种方便访问action的方法“通配符”“动态配置” 通配符:可以使用* 和{1}来优化配置 动态配置这个访问方式action名字action类中的需要访问的方法名例如hello!add.action /struct2/hello!add.action:用这个只有在常量设置中设置 /{1}.jsp 2)路径匹配原则 /Struts2_01/hello_a/a/b/helloWorld.action /Struts2_01/hello_a/a/b找package-没找到/Struts2_01/hello_a/a-》没找到/Struts2_01/hello_a没找到/Struts2_01/-报404错 3)常量 所有的初始化全局变量配置都在Structs-core-2.3.4-1.jar/org.apache.structs/default.properities # # $Id: default.properties 1132110 2011-06-05 08:45:32Z lukaszlenart $ # # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # License); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # ### START SNIPPET: complete_file ### Struts default properties ###(can be overridden by a struts.properties file in the root of the classpath) ### ### Specifies the Configuration used to configure Struts ### one could extend org.apache.struts2.config.Configuration ### to build ones customize way of getting the configurations parameters into Struts # struts.configurationorg.apache.struts2.config.DefaultConfiguration ### This can be used to set your default locale and encoding scheme # struts.localeen_US struts.i18n.encodingUTF-8 ### if specified, the default object factory can be overridden here ### Note: short-hand notation is supported in some cases, such as spring ### Alternatively, you can provide a com.opensymphony.xwork2.ObjectFactory subclass name here # struts.objectFactory spring ### specifies the autoWiring logic when using the SpringObjectFactory. ### valid values are: name, type, auto, and constructor (name is the default) struts.objectFactory.spring.autoWire name ### indicates to the struts-spring integration if Class instances should be cached ### this should, until a future Spring release makes it possible, be left as true ### unless you know exactly what you are doing! ### valid values are: true, false (true is the default) struts.objectFactory.spring.useClassCache true ### ensures the autowire strategy is always respected. ### valid values are: true, false (false is the default) struts.objectFactory.spring.autoWire.alwaysRespect false ### if specified, the default object type determiner can be overridden here ### Note: short-hand notation is supported in some cases, such as tiger or notiger ### Alternatively, you can provide a com.opensymphony.xwork2.util.ObjectTypeDeterminer implementation name here ### Note: By default, com.opensymphony.xwork2.util.DefaultObjectTypeDeterminer is used which handles type detection ### using generics. com.opensymphony.xwork2.util.GenericsObjectTypeDeterminer was deprecated since XWork 2, its ### functions are integrated in DefaultObjectTypeDeterminer now. ### To disable tiger support use the notiger property value here. #struts.objectTypeDeterminer tiger #struts.objectTypeDeterminer notiger ### Parser to handle HTTP POST requests, encoded using the MIME-type multipart/form-data # struts.multipart.parsercos # struts.multipart.parserpell struts.multipart.parserjakarta # uses javax.servlet.context.tempdir by default struts.multipart.saveDir struts.multipart.maxSize2097152 ### Load custom property files (does not override struts.properties!) # struts.custom.propertiesapplication,org/apache/struts2/extension/custom ### How request URLs are mapped to and from actions #struts.mapper.classorg.apache.struts2.dispatcher.mapper.DefaultActionMapper ### Used by the DefaultActionMapper ### You may provide a comma separated list, e.g. struts.action.extensionaction,jnlp,do ### The blank extension allows you to match directory listings as well as pure action names ### without interfering with static resources, which can be specified as an empty string ### prior to a comma e.g. struts.action.extension, or struts.action.extensionx,y,z,, struts.action.extensionaction,, ### Used by FilterDispatcher ### If true then Struts serves static content from inside its jar. ### If false then the static content must be available at/struts struts.serve.statictrue ### Used by FilterDispatcher ### This is good for development where one wants changes to the static content be ### fetch on each request. ### NOTE: This will only have effect if struts.serve.statictrue ### If true - Struts will write out header for static contents such that they will ### be cached by web browsers (using Date, Cache-Content, Pragma, Expires) ### headers). ### If false - Struts will write out header for static contents such that they are ### NOT to be cached by web browser (using Cache-Content, Pragma, Expires ### headers) struts.serve.static.browserCachetrue ### Set this to false if you wish to disable implicit dynamic method invocation ### via the URL request. This includes URLs like foo!bar.action, as well as params ### like method:bar (but not action:foo). ### An alternative to implicit dynamic method invocation is to use wildcard ### mappings, such asstruts.enable.DynamicMethodInvocation true ### Set this to true if you wish to allow slashes in your action names. If false, ### Actions names cannot have slashes, and will be accessible via any directory ### prefix. This is the traditional behavior expected of WebWork applications. ### Setting to true is useful when you want to use wildcards and store values ### in the URL, to be extracted by wildcard patterns, such as ###to match /foo/edit or ### /foo/save. struts.enable.SlashesInActionNames false ### use alternative syntax that requires %{} in most places ### to evaluate expressions for String attributes for tags struts.tag.altSyntaxtrue ### when set to true, Struts will act much more friendly for developers. This ### includes: ### - struts.i18n.reload true ### - struts.configuration.xml.reload true ### - raising various debug or ignorable problems to errors ### For example: normally a request to foo.action?someUnknownFieldtrue should ### be ignored (given that any value can come from the web and it ### should not be trusted). However, during development, it may be ### useful to know when these errors are happening and be told of ### them right away. struts.devMode false ### when set to true, resource bundles will be reloaded on _every_ request. ### this is good during development, but should never be used in production struts.i18n.reloadfalse ### Standard UI theme ### Change this to reflect which path should be used for JSP control tag templates by default struts.ui.themexhtml struts.ui.templateDirtemplate #sets the default template type. Either ftl, vm, or jsp struts.ui.templateSuffixftl ### Configuration reloading ### This will cause the configuration to reload struts.xml when it is changed struts.configuration.xml.reloadfalse ### Location of velocity.properties file. defaults to velocity.properties struts.velocity.configfile velocity.properties ### Comma separated list of VelocityContext classnames to chain to the StrutsVelocityContext struts.velocity.contexts ### Location of the velocity toolbox struts.velocity.toolboxlocation ### used to build URLs, such as the UrlTag struts.url.http.port 80 struts.url.https.port 443 ### possible values are: none, get or all struts.url.includeParams none ### Load custom default resource bundles # struts.custom.i18n.resourcestestmessages,testmessages2 ### workaround for some app servers that dont handle HttpServletRequest.getParameterMap() ### often used for WebLogic, Orion, and OC4J struts.dispatcher.parametersWorkaround false ### configure the Freemarker Manager class to be used ### Allows user to plug-in customised Freemarker Manager if necessary ### MUST extends off org.apache.struts2.views.freemarker.FreemarkerManager #struts.freemarker.manager.classnameorg.apache.struts2.views.freemarker.FreemarkerManager ### Enables caching of FreeMarker templates ### Has the same effect as copying the templates under WEB_APP/templates struts.freemarker.templatesCachefalse ### Enables caching of models on the BeanWrapper struts.freemarker.beanwrapperCachefalse ### See the StrutsBeanWrapper javadocs for more information struts.freemarker.wrapper.altMaptrue ### maxStrongSize for MruCacheStorage for freemarker struts.freemarker.mru.max.strong.size100 ### configure the XSLTResult class to use stylesheet caching. ### Set to true for developers and false for production. struts.xslt.nocachefalse ### Whether to always select the namespace to be everything before the last slash or not struts.mapper.alwaysSelectFullNamespacefalse ### Whether to allow static method access in OGNL expressions or not struts.ognl.allowStaticMethodAccessfalse ### Whether to throw a RuntimeException when a property is not found ### in an expression, or when the expression evaluation fails struts.el.throwExceptionOnFailurefalse ### Logs as Warnings properties that are not found (very verbose) struts.ognl.logMissingPropertiesfalse ### Caches parsed OGNL expressions, but can lead to memory leaks ### if the application generates a lot of different expressions struts.ognl.enableExpressionCachetrue ### END SNIPPET: complete_file View Code 1.Struct中默认访问后缀 Struct1:.do;Struct2:.action 2.在Struct.xml通过常量修改 指定访问后缀为action/do/没有访问后缀都可以 valueaction,do,不带后缀 valueaction,do访问后缀action或do valueaction 后缀只能是action 3.常量在struct.xml中配置 4)resultType 1.在action中想要获得request对象 ServletActionContext.getRequest();ServletActionContext是action重要对象 2.其中type的设置有 3.result全局结果 当多个action中使用了相同的result为了避免result的重复我们可以设置全局结果但是局部权限大于全局 4.数据封装 1)struts对数据封装当访问action时参与核心过滤器访问default-struts.xml-strut.xml在default-struts.xml中有32个拦截器其中Parameters拦截器对数据进行封装 2)String-》基本数据类型转换是自动的 String-》Date日期类型的转换是有条件的 3)转换原理过程 1.表单中的name值自动映射到Action中的一个属性2.还可以映射到一个集合 3.Struts对HttpServetRequest,HttpSession,ServletContext进行了封装构造了三个Map对象可通过ServletActionContext来访问三个对象 4)在action中两种获得数据的方法 1.ServletApi 2.通过ServletActionContext获得ActionContext对象得到三个封装好的Map对象3.实现三个接口RequestAware, SessionAware, ApplicationAwar也可以 方式一通过Servlet Api HttpServletRequest request ServletActionContext.getRequest(); HttpSession sessionrequest.getSession(); ServletContext applicationServletActionContext.getServletContext();//操作 request.setAttribute(request_data, request_data1); session.setAttribute(session_data, session_data1); application.setAttribute(application_data, application_data1); 方式二推荐这个方法 //Struts中对数据操作方式2 通过ActionContext类 ActionContext ac ActionContext.getContext();//得到Struts对HttpServletRequest对象进行了封装封装为一个map//拿到表示request对象的map Map request ac.getContextMap();//拿到表示session对象的map Map session ac.getSession();//拿到表示servletContext对象的map Map application ac.getApplication();//数据 request.put(request_data, request_data1_actionContext); session.put(session_data, session_data1_actionContext); application.put(application_data, application_data1_actionContext); /** * 数据处理, 方式3 实现接口的方法 * author Jie.Yuan **/ public classDataAction extends ActionSupport implements RequestAware, SessionAware, ApplicationAware{private Maprequest;private Mapsession;private Mapapplication;//struts运行时候会把代表request的map对象注入 public void setRequest(Maprequest) {this.request request; }//注入session public void setSession(Mapsession) {this.session session; }//注入application public void setApplication(Mapapplication) {this.application application; }publicString execute() throws Exception {//数据 /** // Struts中对数据操作方式1 直接拿到ServletApi, 执行操作 HttpServletRequest request ServletActionContext.getRequest(); HttpSession session request.getSession(); ServletContext application ServletActionContext.getServletContext(); // 操作 request.setAttribute(request_data, request_data1); session.setAttribute(session_data, session_data1); application.setAttribute(application_data, application_data1);*/ //【推荐解耦的方式实现对数据的操作】//Struts中对数据操作方式2 通过ActionContext类 ActionContext ac ActionContext.getContext();//得到Struts对HttpServletRequest对象进行了封装封装为一个map//拿到表示request对象的map Map request ac.getContextMap();//拿到表示session对象的map Map session ac.getSession();//拿到表示servletContext对象的map Map application ac.getApplication();//数据 request.put(request_data, request_data1_actionContext); session.put(session_data, session_data1_actionContext); application.put(application_data, application_data1_actionContext);// returnSUCCESS; } } View Code 5.类型转换 前面说了数据转换String-基本类型类型自动的日期需要条件 1)是Parameters拦截器做的类似于: Beanutils工具 2)自定义类型转换器 1.继承StrutsTypeConverter 2.全局转换局部转换配置 例子 /** * 自定义类型转换器类 * * author Jie.Yuan **/ public classMyConverter extends StrutsTypeConverter {//新需求 要求项目中要支持的格式,如: yyyy-MM-dd/yyyyMMdd/yyyy年MM月dd日..//先定义项目中支持的转换的格式 DateFormat[] df { new SimpleDateFormat(yyyy-MM-dd),new SimpleDateFormat(yyyyMMdd),new SimpleDateFormat(yyyy年MM月dd日) };/** * 把String转换为指定的类型 【String To Date】 * * param context * 当前上下文环境 * param values * jsp表单提交的字符串的值 * param toClass * 要转换为的目标类型*/OverridepublicObject convertFromString(Map context, String[] values, Class toClass) {//判断: 内容不能为空 if (values null || values.length 0) {return null; }//判断类型必须为Date if (Date.class !toClass) {return null; }//迭代转换失败继续下一个格式的转换 转换成功就直接返回 for (int i0; i }catch(ParseException e) {continue; } }return null; } OverridepublicString convertToString(Map context, Object o) {return null; } } View Code 局部配置在自定义转化器添加(自定义转换器名字-conversion.properties):MyConverter-conversion.properties 在其中写需要转换的字段名自定义转换器类的权限定名birthtype.MyConverter 全局配置在项目src目录下建立固定文件xwork-conversion.properties 在其中写需要转换的类类型转换器类的权限定名 java.util.Datetype.MyConverter(java.util.Date是birth的类型) 3)struts-default.xml 该拦截器负责对错误信息处理 6.文件支持 1)文件上传 1.核心类FileItemFactory;ServletFileUpload;FileItem 2.struts处理上传文件: 获得上传文件的file,对应的fileName,fileContextType之后再execute中执行对上传文件的处理 上传文件默认都是被缓存到.me_tcat\work\Catalina\localhost\struts02\upload_5bd0b60c_15c3369eeb3__8000_00000005.tmp中 我们要做的是把文件得到存到我们指定的位置 3.配置action:在action子栏中添加。。。等信息 在default-struts中有拦截器fileUpload而在ServletFileUpload类中文件属性我们都可以在自己写的拦截器中添加 txt,jpg,jar 1024 当文件上传错误时会自动放回字符串input,我们可以在action中配置跳转到指定页面这个页面想要显示错误信息可以通过引用struts标签显示 4.上传例子 1.jsp用户名: 文件: 2.actionpublic classFileUpload extends ActionSupport {/** **/ private static final long serialVersionUID 1L;//对应表单 privateFile file1;//文件名 privateString file1FileName;//文件的类型(MIME) privateString file1ContentType;public voidsetFile1(File file1) {this.file1 file1; }public voidsetFile1FileName(String file1FileName) {this.file1FileName file1FileName; }public voidsetFile1ContentType(String file1ContentType) {this.file1ContentType file1ContentType; }//上面的代码是通过拦截器对数据自动封装到这三个属性中的 OverridepublicString execute() throws Exception {/******拿到上传的文件进行处理******/ //把文件上传到upload目录 System.out.println(file1FileName); System.out.println(file1ContentType);//获取上传的目录路径 String path ServletActionContext.getServletContext().getRealPath(/upload); System.out.println(path);//创建目标文件对象 File destFile newFile(path,file1FileName);//把上传的文件拷贝到目标文件中 FileUtils.copyFile(file1, destFile);returnSUCCESS; } } View Code 2)文件下载 访问连接down_down.action就是到down_list的action中找到类对应的list方法下面的例子是list方法返回list字符串在action中就找到对于的result放回到list.jsp中在这里通过点击下载-》又发送连接down_down?fileName文件名.txt-》找到down_down的action找到对应的类里面的down方法开始下载-》随后在对应的action中找到result这个result的为属于下载业务功能需要特殊设置 typestream;之后在子项中添加四个param子标签 application/octet-stream attrInputStream attachment;filename${downFileName} 1024 之后我们需要在action类中再写 1.放回流属性的getAttrInputStream()方法放回要下载文件的流 2.返回下载头包含浏览器显示的文件名:getDownFileName() 上传下载的整体例子 上传 1.file.xml中的配置?xml version1.0 encodingUTF-8 ? /p http://struts.apache.org/dtds/struts-2.0.dtd txt,jpg,jar 2097152 /e/success.jsp /e/error.jsp e/list.jsp application/octet-stream attrInputStream attachment;filename${downFileName} 1024 2.FileUpload.java代码 package fileupload; import java.io.File; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport;/** * 文件上传 * author Administrator **/ public classFileUpload extends ActionSupport{/** **/ private static final long serialVersionUID 1L;privateFile file1;privateString file1FileName;//上面的代码是通过拦截器对数据自动封装到这三个属性中的 public voidsetFile1(File file1) {this.file1 file1; }public voidsetFile1FileName(String file1FileName) {this.file1FileName file1FileName; } OverridepublicString execute() throws Exception { String pathServletActionContext.getServletContext().getRealPath(/upload); File destDirnewFile(path,file1FileName); FileUtils.copyDirectory(file1, destDir); System.out.println(上传成功);returnSUCCESS; } }3.提交上传的表单jspInsert title here文件 View Code 下载 1.DownAction.java package fileupload; import java.io.File; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Map; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport;public classDownAction extends ActionSupport{/** **/ private static final long serialVersionUID 1L;/************显示所有需要下载的文件*************/ publicString list(){ String pathServletActionContext.getServletContext().getRealPath(/upload); File filenewFile(path); String[] fileNames file.list();//得到所有下载的文件名//保存 ActionContext acServletActionContext.getContext(); Map request(Map) ac.get(request); request.put(fileNames, fileNames);return list; }/***************2.文件下载****************/ /*1。获得要下载的文件名字*/ privateString fileName;public voidsetFileName(String fileName){//这里提交时get提交存在乱码问题需要解决下 try{ fileNamenew String(fileName.getBytes(ISO8859-1),UTF-8); }catch(UnsupportedEncodingException e) {//TODO Auto-generated catch block e.printStackTrace(); }this.fileNamefileName; }/*2.下载提交业务*/ publicString down(){return download; }/*3.返回文件流的方法*/ publicInputStream getAttrInputStream(){return ServletActionContext.getServletContext().getResourceAsStream(/uploadfileName); }/*4.下载显示的文件名*/ publicString getDownFileName(){//需要中文编码 try{ fileNameURLEncoder.encode(fileName,UTF-8); }catch(UnsupportedEncodingException e) {//TODO Auto-generated catch block e.printStackTrace(); }returnfileName; } }2.显示所有下载文件的list.jsp String basePath request.getScheme()://request.getServerName():request.getServerPort()path/;%下载列表 编号文件名操作${vs.count}${fileName}下载 3。访问方式先访问list,再下载 View Code 6.拦截器 a) 想出拦截器原因 用户想要给action什么功能的时候可以通过拦截器自由组装基于组件的设计 b) 知识点拦截器在struts-default中定义了32种拦截器18中默认拦截器 拦截器栈组合多个拦截器默认使用strut-default的18个默认拦截器defaultStack 一旦用户指定哪个拦截器默认拦截器就不起作用了 c) 拦截器的配置 在struts-default中定义所有的拦截器其中默认拦截器 dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...* input,back,cancel,browse input,back,cancel,browse View Code 自己在struts中定义拦截器 1.定义拦截器和拦截器栈 1.1定义拦截器 1.2定义拦截器栈 引用上面的或其他的拦截器 2.默认执行拦截器(栈) e) 拦截器核心api Interceptor接口 AbstractInterceptor 拦截器默认实现的抽象类一般自定义开发继承它就行了 ActionInvacation 拦截器的执行状态调用下一个拦截器或者action f) 拦截器和过滤器的区别 g) 拦截器的生命周期 服务器启动-》过滤器创建-》初始化init()创建所有拦截器对象-》客户端访问-》创建action实例-》拦截器interceptor方法拦截-》下一个拦截-》。。。》到达action执行execute()-返回给用户结果 h) 自定义拦截器例子 1.写一个拦截器类 package interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor;/** * 拦截器定义 * author Administrator **/ public classHelloInterceptor implements Interceptor{/** **/ private static final long serialVersionUID 1L; Overridepublic voiddestroy() {//TODO Auto-generated method stub System.out.println(销毁。。。。。); } Overridepublic voidinit() {//TODO Auto-generated method stub System.out.println(自定义拦截器初始化成功); }/** * 拦截器业务处理在访问action时执行在excute之前执行*/OverridepublicString intercept(ActionInvocation arg0) throws Exception { System.out.println(拦截器开始执行);//执行业务逻辑//执行下一个拦截器 String aaarg0.invoke();//拦截器结束 System.out.println(拦截器结束);returnaa; } } View Code 2.配置struts配置拦截器分3步 a) 定义自定义拦截器 b) 定义我们的拦截器栈:其中引用默认拦截器栈要放在第一位 c) 执行拦截器 /index.jsp View Code 7.国际化 a) Serlvelt中的国际化 写资源文件 基础名.properties【默认的语言环境设置】 基础名_语言简称_国家简称.properties 读取资源文件再使用 程序ResourceBundle Jsp: jstl提供的格式化与国际化标签库。 b) Struts中的国际化 写资源文件(同servlet) 读取资源文件再使用 程序ResourceBundle Jsp:  1)jstl表亲啊  (同servlet) 2)struts标签获取资源文件内容 c) 注意 还可以在页面加载  标签必须放到标签体中。 d) 区别 Struts2加载资源文件更加简单通过常量加载即可再在jsp页面直接使用 à1.  写资源文件 Msg.properties   默认的语言环境 找不到配置就找它 Msg_en_US.properties  美国 -à2.  加载 à3. 使用:标签name值直接写配置文件中的key 8.Ognl表达式语言与Struts标签 深刻理解Ognl,Struct2传输模式 http://blog.sina.com.cn/s/blog_7ffb8dd5010141pd.html 1.ActionContext装饰OgnlContext2.OgnlValueStack值栈对象包含了OgnlContext,root对象3.OgnlContext中包含了所有域对象全局属性action对象等等4.Struts2数据传输DataTransfer的核心对象是OgnlValueStack、 OgnlContext 这里主要需要知道的是“值栈对象”生成过程和内部成员调用方式 a) Ognl表达式Object Graphic Navigation Language(对象图导航语言)的缩写是一个开源项 b) 优势 支持对象方法调用如xxx.doSomeSpecial() 支持类静态的方法调用和值访问表达式的格式[类全名(包括包路径)][方法名|值名]例如java.lang.Stringformat(foo %s, bar) 支持赋值操作和表达式串联如price100, discount0.8,calculatePrice()这个表达式会返回80 访问OGNL上下文(OGNL context)和ActionContext 操作集合对象 c) OgnlContext对象 是Ognl表达式的核心 访问Ognl表达式中值Ognl.getValue(ognl表达式ActionContext ac,ognl的root) 例子主要看我们对ActionContext上下文对象如何处理ac中直接添加键值对创建ognl表达式时要加#ac添加root时创建表达式就不需要了 当我们调用工具类的一个方法时创建ognl表达式Ognl.parseExpression([类名][方法调用])如Mathfloor(10.9) 1. Ognl表达式语言语言取值取非根元素的值必须用#号 public voidtestOgnl() throws Exception {//创建一个Ognl上下文对象 OgnlContext context newOgnlContext();//放入数据 User user newUser(); user.setId(100); user.setName(Jack);//【往非根元素放入数据 取值的时候表达式要用#】 context.put(user, user);//获取数据(map)//先构建一个Ognl表达式, 再解析表达式 Object ognl Ognl.parseExpression(#user.name); Object valueOgnl.getValue(ognl, context, context.getRoot()); System.out.println(value); } View Code 2. Ognl表达式语言语言取值取根元素的值不用带#号 public voidtestOgn2() throws Exception {//创建一个Ognl上下文对象 OgnlContext context newOgnlContext();//放入数据 User user newUser(); user.setId(100); user.setName(Jack);//【往根元素放入数据】 context.setRoot(user);//获取数据(map)//先构建一个Ognl表达式, 再解析表达式 Object ognl Ognl.parseExpression(address.province); Object valueOgnl.getValue(ognl, context, context.getRoot()); System.out.println(value); } View Code 3.Ognl对 静态方法调用的支持 public voidtestOgn3() throws Exception {//创建一个Ognl上下文对象 OgnlContext context newOgnlContext();//Ognl表单式语言调用类的静态方法//Object ognl Ognl.parseExpression(Mathfloor(10.9));//由于Math类在开发中比较常用所以也可以这样写 Object ognl Ognl.parseExpression(floor(10.9)); Object valueOgnl.getValue(ognl, context, context.getRoot()); System.out.println(value); } View Code 4.Ognl运用在jsp页面数据存到request中后自然就存到OgnlContext中了在jsp页面通过#request.list就能访问到OgnlContext中的list对象和里面的值OgnlContext就相当于一个容器存放了所有的域对象和自己存进去的键值对 map迭代 View Code 不是根Obnl的根对象都是用#开头才能访问到 d) ValueStack对象 用户访问action时创建action对象之后再将很多信息action对象、全局属性、域对象、等等存入值栈对象中之后值栈对象就有了很多内容我们取值都可以从这里取 值栈对象是struts数据存储中心或者说是中转站 访问action时创建了action对象、值栈对象、ActionContext对象将action对象放入值栈对象再将值栈对象存入request中传到jsp中 ActionContext类装饰OgnlContext类 获取值栈对象2种方式 1.因为action对象放入值栈对象值栈对象又存入request对象所有想要获得ValueStack对象可以从request对象中获得 ValueStack vs1 (ValueStack) request.getAttribute(struts.valueStack); 2.直接从ActionContext对中获得 ac..getValueStack(); e) Struts标签 Struct标签就是用了Ognl表达式语言 9.验证 代码验证 package validation; import com.opensymphony.xwork2.ActionSupport;/** * 验证需要实现接口或者继承ActionSupport * author Administrator **/ public classUserAction extends ActionSupport{private static final long serialVersionUID 1L;//封装数据的请求 privateUser user;publicUser getUser() {returnuser; }public voidsetUser(User user) {this.user user; }//重写数据验证的方法 /*public void validateRegister() { if(user.getUserName()null||.equals(user.getUserName())){ //保存错误信息 super.addFieldError(userName,用户名必须重写); } // 密码 if (user.getPwd() null || .equals(user.getPwd())) { super.addFieldError(pwd, 密码必填); } }*/ //业务方法 publicString register(){ System.out.println(user); System.out.println(1);returnSUCCESS; }//列表展示 publicString list(){returnSUCCESS; } } View Code xml验证 /p http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd 用户名不能为空 密码不能为空 6 8 密码必须为6-8位 日期格式不对 邮件格式不对 View Code 1)验证原理 通过拦截器验证 2)配置验证步骤 写一个类继承ActionSuppert或者实现Validate接口重写validate()方法即可 要注意的是想要验证指定的方法只需只需验证名称规则validate要验证的方法名(public void validateRegister()) 3)验证action的方法 1)代码验证 重写验证方法注意命名规则可以指定特定的方法验证 validate要验证的方法名 public void validateRegister() { 只会验证当前action的register方法 2)XML方式验证 1.将错误信息显示在jsp页面 2.代码验证缺点设计很多重复的验证逻辑例如非空验证、数值验证、email、日期等。 3.Struts对于常用的验证进行了封装 Struts提供的所有的验证器xwork-core-2.3.4.1.jar/com.opensymphony.xwork2.validator.validators/default.xml 都在这里了 View Code XML文件名称语法 指定的是所有验证ActionClassName-validation.xml 指定特定的方法命名规则ActionClassName-ActionName-validation.xml 注意此XML需要与当前要验证的acton同在一个目录 举例UserAction-validation.xml/UserAction-register-validation.xml 例如xml中的规则在dtd文件在xwork-core-2.3.4.1.jar下慢慢找吧dtd文件 /p http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd 用户名不能为空 密码不能为空 6 8 密码必须为6-8位 日期格式不对 邮件格式不对 View Code 3)验证总结 代码 重写validate()   ,  验证action所有方法 Validate方法名(), 验证指定“方法名”的方法 Xml 验证所有方法 ActionClassName-validation.xml 验证指定方法 ActionClassName-actionName-validation.xml 代码验证 比较灵活可以满足所有的需求. 比较繁琐要写重复的验证判断逻辑! 适合 表单字段较少的情况用 XML验证 通用但不够灵活 可以验证特定简单的业务。 适合 验证表单字段较多可以大大简化代码 (配置文件过多) 4)显示错误信息 1.方式一 2.方式二这样弄因为显示错写信息时会自动生成ui,ul标签 ul li{display: inline;color: red;} 3.修改标签定义的模板 找到fielderror标签定义的模板文件Struts-core.jar\template\simple\ fielderror.ftl 把修改后的文件放到src/ template/ simple/fielderror.ftl 这样样式就修改好了 View Code 10.struts简单的UI标签 用户名密码 namestruts.ui.theme valuesimple 11.Struts中几种特殊符号(jsp页面) #获取非根元素值、map集合 $:配置文件取值 %提供一个ognl表示的运行环境 例子 获取request域数据 国家 View Code 12.Struts中常用的几个技术 1)数据回显 必须使用struts标签 进入修改页面时-》将数据信息存入request域Map中或者存入栈值对中在jsp页面通过struts标签就能获得obnl标签值 两种数据存入方式 1.通过request域Map 2.通过ValueStack存入头对象能直接访问 例子 Action中//进入修改页面 publicString viewUpdate() {//模拟一个对象(先获取一个id再根据id调用service查询把查到的结果保存到域) User userInfo newUser(); userInfo.setUserName(Jack); userInfo.setEmail(yuanjieitcast.cn); ActionContext acActionContext.getContext();//Map request (Map) ac.get(request);//request.put(userInfo, userInfo); /************* 数据回显***************/ //获取值栈 ValueStack vs ac.getValueStack(); vs.pop();//移除栈顶元素 vs.push(userInfo); //入栈//进入修改页面 return viewUpdate; } JSP页面 用户名 邮箱 View Code 2)模型驱动属性驱动 模型驱动就是直接将对象封装属性驱动就是将属性赋值给对象的属性 OverridepublicString intercept(ActionInvocation invocation) throws Exception { Object actioninvocation.getAction();if(action instanceof ModelDriven) { ModelDriven modelDriven(ModelDriven) action; ValueStack stackinvocation.getStack(); Object modelmodelDriven.getModel();if (model ! null) { stack.push(model); }if(refreshModelBeforeResult) { invocation.addPreResultListener(newRefreshModelBeforeResult(modelDriven, model)); } }returninvocation.invoke(); } View Code prams拦截器可以把请求数据自动填充的action的属性中 就是数据封装的关键拦截器 4.表单重复提交拦截器 Struts2知识进阶 1.继承了ActionSupport的类想要给前台页面发送信息处了可以通过域对象发送还可以通过以下三种方式提交信息 this.addActionMessage(message); this.addActionError(anErrorMessage); this.addFieldError(fieldName, errorMessage) 之后在前台界面使用 来显示数据信息
http://www.w-s-a.com/news/801291/

相关文章:

  • 网站根 html网站建设行业数据
  • 网站公司做的网站有最字设计说明室内设计
  • 在线网站代码生成我想做个百度网站怎么做
  • 网站的建设费用分为长治市建设厅官方网站
  • 做网站都有哪些费用建设免费手机网站
  • 网站 组成代码做网站图片怎么插
  • 2020中国企业500强榜单南宁seo标准
  • 北美购物网站排名烟台专业的网站建站公司
  • 门户网站设计特点营销策划咨询机构
  • 天津做网站就到徽信xiala5中国营销型网站
  • 外汇网站建设制作深圳三站合一网站建设
  • 深圳坂田网站设计公司有哪些学校网站建设管理办法
  • 太原建设银行网站中山营销型网站设计
  • 广东省建设厅官方网站多少钱江苏省江建集团有限公司建设网站
  • 网站开发主流服装网站开发课程设计
  • 在iis里面创建网站wordpress响应式视频
  • 学设计哪个网站好网页设计音乐网站
  • 可以自己做斗图的网站上海模板建站多少钱
  • 山东川畅信息技术有限公司网站建设网站开发任务书
  • 网站排版设计欣赏搭建公司介绍网站
  • 网站弹窗是怎么做的长沙智优营家
  • 手机网站菜单设计模板菜单网站图片素材
  • 浙江网站推广爱企查企业查询入口
  • 公司网站平台vs2012网站开发课程设计
  • 哪些方法可以建设网站做网站失败
  • 龙岗网站建设技术wordpress左右两栏
  • 电子商务网站开发与应用的介绍怎么查询域名是否备案
  • 想做一个自己设计公司的网站怎么做的权威发布型舆情回应
  • 做ppt用的音效网站python基础教程网易
  • 可以做免费广告的网站有哪些做视频赚钱的国外网站