Action开发、通配符、路径问题和struts中常量用法
1、action开发
开发的几种方式
(1)、继承自ActionSupport,(如果用struts的数据效验功,能必须必须使用此功能,因为ActionSupport实现了数据效验的接口)
public class UserAction extends ActionSupport{}
(2)、实现Action接口,该接口的内容如下。(有五个常量和一个方法)
pubic interface Action{
public static final String success="success";
public static final String NONE="none";
public static final String INPUT="input";
public static final String LOGIN="login";
public Strng execute() throws Exception;
}
(3)、不实现任何类,也不继承任何接口。
public class UserAction3{
//action 中业务处理方法
public String login(){
System.out.println("UserAction Log(3)!");
return "success";
} }
2、访问通配符
在struts的配置信息中可以用*代表与{1}来优化
刚开始的时候是这样写的
<struts>
<package name="config" namespace="/" extends="struts-default">
<action name="login" class="com.gqx.a_test.UserAction" method="login">
<result name="success">/index.jsp</result>
</action>
<action name="register" class="com.gqx.a_test.UserAction" method="register">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
现在可以使用通配符去优化
<!-- 使用通配符去优化 -->
<action name="user_*" class="com.gqx.a_test.UserAction" method="{1}">
<result name="success">/index.jsp</result>
</action>
结果如下图
现在当在action中返回字符串的结果不相同的时候,可以这样写
public class UserAction extends ActionSupport {
//action 中业务处理方法
public String login(){
System.out.println("UserAction Log()!");
return "login";
}
public String register(){
System.out.println("UserAction Register()!");
return "register";
}
}
同时返回的jsp页面不同,这个时候可以这样去配置struct.xml
<action name="user_*" class="com.gqx.a_test.UserAction" method="{1}">
<result name="{1}">/{1}.jsp</result>
</action>
结果如图:
3、struts中路径匹配原则
一般的默认namespace="/",当你改成
<package name="config" namespace="/user" extends="struts-default">
......
</package>
就需要访问这个路径http://localhost:8080/Struts_Study/user/user_register
这时候你还可以这样访问http://localhost:8080/Struts_Study/user/a/b/user_register,结果是正常的
但是这样就不行了 http://localhost:8080/Struts_Study/a/b/user/user_register
究其原因是因为当用户在地址栏输入http://localhost:8080/Struts_Study/user/a/b/user_register,回车后
在服务器Tomcat端
localhost 找到访问那一台机器
8080 根据端口号,找到tomcat
Struts_Study 找到项目名称
/user/a/b 寻找是否有这个名称空间,没找到,继续向下(找到就返回)
/user/a 寻找是否有这个名称空间,没找到,继续向下(找到就返回)
/user 寻找是否有这个名称空间,没找到,继续向下(找到就返回)
/ 默认命名空间,还没有找到,报错
1、常量
struts中默认访问后缀
在struts1中是do,在struts2中是action(以下都可以)
那么如何修改默认访问的后缀
struts2的默认后缀在struts-core-2.3.4-1/org.apache.struts/default.properties中有如下代码
struts.action.extension=action,,
在struts中通过常量去修改
<!-- 全局配置 -->
<!-- 修改struts默认的访问后缀 -->
<constant name="struts.action.extension" value="action,do,,"></constant>
指定访问后缀为do,action或没有都可以
当 value="action,do" 访问后缀只能是action或do
当 value="action" 访问后缀只能是action
Struts2中常用的常量
指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法 和freemarker 、velocity的输出
<constant name="struts.i18n.encoding" value="UTF-8"/>
自定义后缀修改常量
<constant name="struts.action.extension" value="action,,"/>
设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭
<constant name="struts.serve.static.browserCache" value="false"/>
当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开
<constant name="struts.configuration.xml.reload" value="true"/>
开发模式下使用,这样可以打印出更详细的错误信息
<constant name="struts.devMode" value="true" />
默认的视图主题
<constant name="struts.ui.theme" value="simple" />
与spring集成时,指定由spring负责action对象的创建
<constant name="struts.objectFactory" value="spring" />
该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性
为 false
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
上传文件的大小限制
<constant name="struts.multipart.maxSize" value=“10701096"/>
动态方法调用示例,首先在配置文件加上
<!-- 动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
在action中写入
<!-- 动态方法调用上-->
<action name="user" class="com.gqx.b_test.UserAction">
<result name="success">/index.jsp</result>
</action>
在浏览器端地址栏可以输入:http://win-29gp9rls36l:8080/Struts_Study/user!login(感叹号后放方法,一般不会这样写,因为这样写要对类特别的了解,同时缺少安全性)
2、全局跳转视图配置、配置的各项默认值
(1)在struts.xml中配置全局视图(注意:global-results必须放在action的前面。)
<!-- 配置全局跳转视图-->
<global-results>
<result name="success">/index.jsp</result>
</global-results>
<action name="user_*" class="com.gqx.b_test.UserAction" method="{1}">
<!-- 返回结果标志success对应的页面在当前页action中没有配置,
所以会去找全局配置中是否有success标记的页面 -->
</action>
(2)配置各项默认值
<!-- 配置各项默认值 -->
<!--
只配置访问路径名称
class 默认执行的action在struts-default.xml中有配置
<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
该ActionSupport中有方法execute()为默认的方法,同时会返回success,然后会去全局找success
-->
<action name="test"> </action>
(3)WEB-INFO下的资源只能通过转发去访问,不能通过重定向去访问。
js:选项卡的制作
<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>选项卡</title>
<style type="text/css" media="screen">
#div1 div {
width: 200px;
height: 200px;
border: 1px #000 solid;
display: none
} #div1 input.active {
background: red;
}
</style>
</head>
<div id="div1">
<input class="active" type="button" value="1">
<input type="button" value="2">
<input type="button" value="3">
<div style="display:block">1111选项卡</div>
<div>2222选项卡</div>
<div>3333选项卡</div>
</div>
<script type="text/javascript">
var oDiv=document.getElementById('div1');
var oBtn=oDiv.getElementsByTagName('input');
var aDiv=oDiv.getElementsByTagName('div');
// 当需要外层的循环的i值去作为内层循环的一个参照的时候需要这只索引
for (var i = oBtn.length - 1; i >= 0; i--) {
oBtn[i].index=i;
oBtn[i].onclick=function () {
for (var i = oBtn.length - 1; i >= 0; i--) {
oBtn[i].className='';
aDiv[i].style.display='none';
}
this.className='active';
aDiv[this.index].style.display='block';
}
}
</script> <body>
</body> </html>
Action开发、通配符、路径问题和struts中常量用法的更多相关文章
- android开发学习 ------- 枚举类型在Android中的用法
一般上为了简化代码,重用代码,设置标志位来表示不同的流程,这个标志位可以使用枚举类型来表示: 1:定义 public FbManner fbManer = FbManner.EMAIL; //给一个默 ...
- Struts中的常量
以下是Struts中常量的一些经常使用配置,保存下来留作积累吧. <?xml version="1.0" encoding="UTF-8"?> &l ...
- struts开发<struts中的action详细配置. 二>
在eclipse中新建项目StrutsDemo1[struts的配置见]struts开发<在eclipse中配置struts. 一> 详细文件夹结构例如以下 第一种配置方法 新建UserA ...
- Struts 之 通配符 路径匹配 常量用法 配置默认值
Struts 框架学习 Action的开发的几种方式 方式1 : 继承ActionSupport 如果使用Struts校验功能,必须继承此类 方式2 : 实现Action接口 方式3 :不继承 ...
- struts中的常量,action配置中的默认值
1.struts中Action的开发方式 继承ActionSupport类,这种方法实现的Action可以进行数据校验: 实现Action接口: 不继承任何类,不实现任何接口: 是否继承类或实现接口, ...
- struts 中自定义action访问方法
struts中action类继承了ActionSupport 默认实现了execute()方法 struts.xml配置文件中 然后可以配置如下映射: <package name =" ...
- 关于struts中的表单元素- Form bean not specified on mapping for action: "helloa.do"报错
今天测试struts时仿照书上写了一个小的表单提交代码 <html:form action="helloa.do" method="post"> & ...
- Action的三种实现方式,struts.xml配置的详细解释及其简单执行过程(二)
勿以恶小而为之,勿以善小而不为--------------------------刘备 劝诸君,多行善事积福报,莫作恶 上一章简单介绍了Struts2的'两个蝴蝶飞,你好' (一),如果没有看过,请观 ...
- Struts中的数据处理的三种方式
Struts中的数据处理的三种方式: public class DataAction extends ActionSupport{ @Override public String execute() ...
随机推荐
- HDFS的命令行操作
1.namenode –format:格式化DFS 文件系统 2.secondaryNameNode: 运行DFS的 SecondaryNameNode 进程 hadoop secondaryname ...
- XST综合、实现过程包含哪些步骤
2013-06-25 18:53:50 在ISE的主界面的处理子窗口的synthesis的工具可以完成下面的任务: 查看RTL原理图(View RTL schematic) 查看技术原理图(View ...
- UVa 753 (二分图最大匹配) A Plug for UNIX
题意: 有n个插座,m个设备以及k种转化器(每种转化器视为有无限个). 转换器A->B可以将A类型的插头转化成B类型的插头,所以可以插在B类型的插座上. 求最少剩多少不匹配的设备. 分析: 抛开 ...
- 8.2/baltic神(水)题
summary:10 bzoj1334: Description N个政党要组成一个联合内阁,每个党都有自己的席位数. 现在希望你找出一种方案,你选中的党的席位数要大于总数的一半,并且联合内阁的席位数 ...
- SCOI2007蜥蜴
Description 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱 上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到 ...
- android源码GIT下载
mkdir device cd device git clone https://android.googlesource.com/device/common.git mkdir htc cd htc ...
- ASP.NET程序中动态修改web.config中的设置项目(后台CS代码)
using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Dra ...
- [Mac][phpMyAdmin] [2002] No such file or directory
我从phpMyAdmin的官网下载了最新版,将它解压到 /Library/WebServer/Documents 下,然后把文件夹改名 phpmyadmin . 接着输入在浏览器中输入 localho ...
- PSR-4——新鲜出炉的PHP规范
FIG 制定的PHP规范,简称PSR,是PHP开发的事实标准. PSR原本有四个规范,分别是: PSR-0 自动加载 PSR-1 基本代码规范 PSR-2 代码样式 PSR-3 日志接口 2013年底 ...
- 【转】android新建项目时 出现appcompat_v7工程错误和红色感叹号
原文网址:http://www.cnblogs.com/xiaozhang2014/p/4109856.html 最近初学android,版本是22.6.0的话,每次创建一个项目就会出现一个appco ...