struts2框架基本操作总结
struts技术说明
一:第一配置开发环境
1.struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
</struts>
2.web.xml文件
<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>
3.导入包
(xwork,struts2-core,commons-fileupdate,commons-io,commons-logging,ognl,freework)
第二基本使用
<package name="defalut" namespace="/demo" extends="struts-default">
<action name="demo1">
<result>/index.jsp</result>
</action>
<action name="Student_*" class="my.struts2.test.Child" method="{1}">
<result name="message">/index.jsp</result>
</action>
</package>
重定向到action(指定其他包名)
<package name="defalut" namespace="/demo" extends="struts-default">
<action name="demo1">
<result>/index.jsp</result>
</action>
<action name="Student_*" class="my.struts2.test.Child" method="{1}">
<result name="message" type="redirectAction">
<param name="actionName">demo1</param>
<param name="namespace">/demo</param>
</result>
</action>
</package>
(同一个包中)
<result name="message" type="redirectAction">demo1</result>
第四格式化输出HTML
<package name="defalut" namespace="/demo" extends="struts-default">
<action name="demo1">
<result>/index.jsp</result>
</action>
<action name="Student_*" class="my.struts2.test.Child" method="{1}">
<result name="message" type="plainText">
<param name="location">/index.jsp</param>
<param name="charSet">UTF-8</param>
</result>
</action>
</package>
第五action 指定参数
<package name="defalut" namespace="/demo" extends="struts-default">
<action name="demo1">
<result>/index.jsp</result>
</action>
<action name="Student_*" class="my.struts2.test.Child" method="{1}">
<param name="nameString">啊silt就会ihg</param>
<result name="message">/index.jsp</result>
</action>
</package>
struts2常用的常量
struts2可以通过修改常量值来改变action的访问后缀。
<constant name="struts.action.extension" value="do,action"/>
struts.properties中的配置如下:
struts.action.extension=do
此外因为常量可以在下面多个配置文件中进行定义,所以我们需要了解struts2加载常量的搜索顺序:
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
如果在多个文件中配置了同一个常量,则后一个文件中配置的常量值会覆盖前面文件中配置的常量值。
常用常量介绍:
·<constant name="struts.i18n.encoding" value="UTF-8" />
指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法和freemarker、velocity的输出
·<constant name="struts.action.extension" value="do" /> 改变action的后缀
·<constant name="struts.configuration.xml.reload" value="true" />
当struts的配置文件修改后,系统是否自动重新加载该文件,默认为false,开发阶段最好打开,这样不用重启服务器。
·<constant name="struts.devMode" value="true" />
开发模式下使用,这样可以答应出更详细的错误信息
·<constant name="struts.multipart.maxSize" value="10701096" />
上传文件的大小限制,value值表示总大小,不是单个文件。
·<constant name="struts.ui.theme" value="simple" />
默认的视图主题
·<constant name="struts.serve.static.browserCache" value="false" />
设置浏览器是否缓存静态内容,默认为true,开发阶段最好关闭
·<constant name="struts.objectFactory" value="spring"/>
与spring集成时,指定由spring负责action对象的创建
·<constant name="struts.enable.DynamicMethodInvocation" value="true" />
该属性设置struts2师傅支持动态方法调用,该属性的默认值是true。如需关闭,则设置value="false"。
为多个项目指定配置文件
<include file=""></include>
类型转换器
在转换器类中继承DefaultTypeConverter 实现方法
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
}
是双向转换,value为要转换的值 toType 为转换成的类型
配置属性文件
位action配置转换文件
filename=ActionClassName-conversion.properties
其中里面内容是:属性名=转换器class类名)
全局类型转换器
filename= xwork-conversion.properties
其中里面的内容
要转换属性的类全名=转换器类全名
访问添加属性
ActionContext对象
example: ActionContext.getContext.getSession.put("name""liu");
ServletActionContext对象
example:HttpServletRequest request= ServletActionContext.getRequest();
文件的上传
用到工具类fileUtils.copy(from,to);
1.在form中指定:enctype="multipart/form-data"
2.可以通过file的属性名+FileName得到文件名,+ContentType文件类型
3.多文件用属性名为数组input name 相同
自定义拦截器
自定义拦截器实现接口Interceptor
通过拦截器return arg0.invoke();'
未通过返回视图名
在Struts.xml中配置拦截
<interceptors>
<interceptor name="permission" class="my.demo1.PermissionInterceptor"></interceptor>
<interceptor-stack name="permissionStack">
<interceptor-ref name="defaultStack"></interceptor-ref>(提供的)
<interceptor-ref name="permission"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="Load_*" class="my.demo1.Load" method="{1}">
<interceptor-ref name="permission"></interceptor-ref>
<result name="success" type="redirect">/index.jsp</result>
</action>
输入校验
在Action中继承ActionSupport
重写validate方法
校验没通过返回"input"视图名
对指定方法校验校验
validate+方法名(首字母大写)
xml实现表单校验
首先需要继承ActionSupport;
没通过返回"input"视图
对所有的action方法进行校验
xml 文件名 actionClassName-validation.xml
对指定的action方法进行校验 actionCalssName-actionName-validation.xml
详细的校验配置看文档
全局视图定义
<global-results>
<result name="message">/WEB-INF/jsp/LoadState.jsp</result>
</global-results>
方法可以直接返回视图名
对于使用其他包的全局视图可以继承他包
sdtruts2 的国际化
1.在struts.xml配置文件中添加
<constant name="struts.custom.i18n.resources" value="Resource" />
Resource_zh_CN.properties(Resource和value对应)
2配置action范围的资源文件
actionClassName_zh_CN.properties
3配置包范围的资源文件package_zh_CN.properties(package固定写法)
显示资源用
<s:text name="user">(占位符)
<s:param>ling</s:param>
<s:param>ssd</s:param>
</s:text>
<s:text name="habit"></s:text>{无占位符}
在表单中通过<s:textfield key="user"></s:textfield>
在action方法中继承Actionsupport 通过this.getText(key,string[]{占位符内容});
struts2框架基本操作总结的更多相关文章
- Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)
1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...
- Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。
1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...
- Struts2框架基础
Struts2框架基础 1.Java的框架 1.1.框架简介 在大型项目开发过程中,经常会使用到一些框架,这样做好的好处是能够提高工作效率,在java中最常用的的框架就是SSH,这其实是三个框架的简称 ...
- Struts2框架简介和示例
struts2框架 Struts2是java web的框架,在Java Web开发中,表示层框架,其核心是通过扩展Servlet来帮助处理http请求. Struct2的基本流程 Struct2的框架 ...
- struts2 框架处理流程
struts2 框架处理流程 流程图如下: 注意:StrutsPrepareAndExecuteFilter替代了2.1.3以前的FilterDispatcher过滤器,使得在执行Action之前可以 ...
- 搭建一个简单struts2框架的登陆
第一步:下载struts2对应的jar包,可以到struts官网下载:http://struts.apache.org/download.cgi#struts252 出于学习的目的,可以把整个完整的压 ...
- Struts2框架之-注解开发
Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...
- Struts2 框架的快速搭建
方便myEclipse 手动配置Struts2框架,写下此文,需要的朋友拿走不谢~ 一.引入JAR包 WEB工程->WebRoot->WEB-INF->lib引入Struts2对应版 ...
- struts2框架 初始别
struts2 是webwork和struts合并而来. 1.下载struts2 说明: Full Distribution: 为完整版下载,建议下载它 Example Applications:st ...
随机推荐
- QQ音乐API
今天分享的是QQ音乐API 搜索歌曲API:http://s.music.qq.com/fcgi-bin/music_search_new_platform?t=0& amp;n={2}&am ...
- 慕课网-安卓工程师初养成-4-3 Java条件语句之多重 if
来源:http://www.imooc.com/code/1355 多重 if 语句,在条件 1 不满足的情况下,才会进行条件 2 的判断:当前面的条件均不成立时,才会执行 else 块内的代码.例如 ...
- $(function(){})与 (function(){})() (function($){})() 的区别
1. $(function(){ }) 或 jQuery(function(){ }) 此函数也可以写成 jQuery(function(){ }), 用于存放操作DOM对象的代码,执行其中代码时DO ...
- C# 操作XML 如果不存在创建 存在直接追加
#region 写操作日志----------------这种格式 //<Log 操作人="Admin" 操作结果="成功" 结果详情="数据导 ...
- 【Spring 1】Spring简介
一.Spring简介 首先,Spring框架是由于软件开发的复杂性而创建的.Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅仅限于服务器端的开 ...
- Oracle笔记 八、PL/SQL跳转/判断/循环语句块
--goto跳转语句 --在goto 后,跳转到相应的语句,然后执行该语句和后面所有语句 begin dbms_output.put_line('goto开始了'); goto c; --不被执行 d ...
- WP_图片管理机制/异步读取网络图片
项目有这样的需求, 要求窗口加载一揽子图片,为了不让UI阻塞太久,采用异步读取后绑定显示的方案. 图片的下载应该采用并发的过程(等待网络响应会很耗时,一张一张的下载,等待时间太长) 图片的下载不能占用 ...
- IEF could not decode Chinese character in IE history well
My friend is working on some case, and she looks not in the mood. I ask her what's going on. She wan ...
- Android事件
1.Java package com.fish.helloworld; import android.app.Activity; import android.graphics.Color; impo ...
- 修改 Ueditor 默认显示的字体大小
默认字体为16px,有点大,为了美观而且一屏可以显示更多内容,可以修改为12px 打开:ueditor.all.min.js 我用的是压缩版 找到如下代码: body{margin:8px;font- ...