Struts2(二)
来源:互联网 发布:php实现分页代码的思路 编辑:程序博客网 时间:2024/05/20 04:13
目录:
1、Struts2框架的运行过程
2、Struts的开发步骤
3、Struts2的国际化支持
4、文件的
1、Struts2框架的运行过程
(1)客户端发送请求
(2)请求先通过ActionContextCleanUp–>FilterDispatcher
(3)FilterDispatcher通过ActionMapper来决定这个Request需要调用哪个Action
(4)如果ActionMapper决定调用某个Action,FilterDispatcher把请求交给ActionProxy
(5)ActionProxy根据ActionMapper和ConfigurationManager找到需要的Action
(6)创建一个ActionInvocation实例
(7)ActionInvocation调用Action,其中会有相关的拦截器i的调用
(8)Action执行完,ActionInvocation创建Result并返回
2、Struts的开发步骤
(1)在web.xml文件中定义核心Filter来拦截用户请求,需要在web.xml中配置的核心Servlet或Filter才能让该框架参与到应用中
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
(2) 定义处理用户请求的Action类
public class UserAction extends ActionSupport { @Override public void validate() { super.validate(); } public UserAction() { super(); } public String saveAction() throws Exception { return "success"; }}
(3)在Struts.xml文件中配置Action。配置Action就是指定那个请求对应哪个Action处理
<package name="myPackage" extends="struts-default"> <action name="save" class="myPackage.UserAction" method="saveAction"> <result name="success">/index.jsp</result> <result name="input">/login.jsp</result> </action> </package>
(4)当Action处理完用户请求后,会返回一个字符串,类似于SUCCESS/INPUT这些,这个字符串需要和指定物理视图资源关联才有用
(5)编写视图文件
3、Struts2的国际化支持
有时简称il8n,用properties配置文件,有四个地方需要国际化:
(1)JSP页面
(2)Action错误信息
(3)转换错误信息
(4)校验错误 信息
简体中文的国际化资源文件xxx_zh_CN.properties:
loginName=\u7528\u6237\u540D
loginPassword=\u5BC6\u7801
loginSubmit=\u767B\u9646
美国英语的国际化资源文件xxx_en_US.properties:
loginName=username
loginPassword=userpassword
loginSubmit=submit
ruts2中加载全局资源文件
struts.xml
<constant name="struts.i18n.encoding" value="UTF-8"/> <constant name="struts.custom.i18n.resources" value="msg"/>
在JSP文件中调用
<s:textfield name="xxx" key="xxx"/>
文件的上传和下载
文件上传主要依赖org.apache.struts2.interceptor.FileUploadInterceptor这个拦截器,把表单设置为post,将enctype设置为multipart/form-data,让浏览器采用二进制流方式处理,默认使用的是Jakarta的Common-FileUpload的文件上传框架
private String username; private File myUpload; private String myFileName; private String myFileType; private String savePath; public String upload(){ String serNewFileName= UUID.randomUUID().toString(); String suffix=myFileName.substring(myFileName.lastIndexOf(".")); serNewFileName+=suffix; try { FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + serNewFileName); myFileName=serNewFileName; FileInputStream fis=new FileInputStream(getMyUpload()); byte[] buffer=new byte[1024]; int len=0; while((len = fis.read(buffer))>0){ fos.write(buffer,0,len); } fos.close(); return SUCCESS; }catch(Exception e){ e.printStackTrace(); }
在struts.xml中配置上传的Action:
<action name="upload" class="myPackage.FileAction" method="upload"> <param name="savePath">/upload.jsp</param> <result name="success">/uploadSuccess.jsp</result> </action>
文件下载:
在struts.xml中配置Action:
<action name="download" class="myPackage.FileDownload"> <param name="inputPath">\</param> <result name="success"type="stream"> <param name="contentType"></param> <param name="inputName"></param> <param name="contentDisposition"></param> <param name="bufferSize"></param> </result> </action>
- 深入struts2(二)
- Struts2 i18n(二)
- struts2学习(二)
- struts2从零开始(二)
- struts2 (二)
- struts2(二)
- Struts2标签(二)
- struts2笔记(二)
- struts2 实战(二)
- Struts2框架(二)
- 深入浅出struts2(二)
- struts2 入门(二)
- struts2(二)
- struts2(二)
- 【struts2】(二)值栈
- Struts2 简介(二)
- struts2(二)
- Java Struts2 (二)
- iOS NSURLSession Https请求
- java日期工具类
- Boom环境的搭建
- HDU 4121 Xiangqi
- 【邮件群发】许可式邮件的几种营销策略
- Struts2(二)
- jquery 限制文本框只能输入数字
- java-Date、SimpleDateFotmat、Calendar
- linux查看防火墙状态及开启关闭命令(转)
- java.util.Date与java.sql.Date 字符串转Date
- git如何更新fork的repository
- mybatis:Invalid bound statement (not found)
- Python爬虫笔记v1.0
- caffe中的train_val.prototxt的文件的理解