Struts2之Action配置的各项默认值、result配置的各种试图转发类型及为应用指定多个str...

[color=red]摘要[/color]:   1、Action配置的各项默认值  (1)、如果没有为action指定class,默认是ActionSupport。  (2)、如果没有为action指定method,默认执行action中的execute()方法。  (3)、如果没有为action指定result,默认值 ...


1、Action配置的各项默认值
  (1)、如果没有为action指定class,默认是ActionSupport。
  (2)、如果没有为action指定method,默认执行action中的execute()方法。
  (3)、如果没有为action指定result,默认值为success。
  2、result配置的各种试图转发类型
  <result type="">...</result>
  其中type的常用类型有:
  dispatcher(默认值) ->转发
  redirect ->重定向
  redirectAction ->重定向的action在别的命名空间下
  如:<result type="redirectAction">
  <param name="actionName">helloworld</param>
  <param name="nameSpace">/test</param>
  </result>
  plainText ->显示原始文件内容
  如:<result type="">
  <param name="location">/xxx.jsp</param>
  <!--指定读取文件的编码-->
  <param name="charSet">UTF-8</param>
  </result>
  注意:在result中还可以使用${属性名}表达式访问action中的属性,表达式里的属性名对应action中的属性名
  如:<result name="success" type="redirect">/index.jsp?username=${username}</result>
  3、为应用指定多个struts配置文件
  在大部分应用里,随着应用规模的增加,系统中Action数量也大量增加,导致struts.xml配置文件变得非常臃肿。为了避免struts.xml文件过于庞大、臃肿,提高struts.xml文件的可读性,我们可以将一个struts.xml配置文件分解成多个配置文件,然后在struts.xml文件中包含其他配置文件。下面的struts.xml通过<include>元素指定多个配置文件:
  <?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>
  <include file="struts-user.xml"/>
  <include file="struts-order.xml"/>
  </struts>
  通过这种方式,我们就可以将Struts 2的Action按模块配置在多个配置文件中。
  以下为模拟案例
  目录结构
  HelloWordAction action类
  package com.ljq.action;
  import java.net.URLEncoder;
  public class HelloWordAction {
  private String msg;
  private String username;
  public String msg(){
  msg = "我的第一个Struts2应用";
  return "msg";
  }
  public String list() throws Exception{
  //对中文进行编码
  msg = "我的第一个Struts2应用";
  this.username=URLEncoder.encode("林计钦","UTF-8");
  return "list";
  }
  public String getMsg() {
  return msg;
  }
  public void setMsg(String msg) {
  this.msg = msg;
  }
  public String getUsername() {
  return username;
  }
  public void setUsername(String username) {
  this.username = username;
  }
  }
  message.jsp页面
  <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  <%
  String path = request.getContextPath();
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  %>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
  <head>
  <base href="<%=basePath%>">
  <title>message</title>
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->
  </head>
  <body>
  <b>${msg }</b>
  </body>
  </html>
  index.jsp页面
  <%@ page language="java" import="java.util.*,java.net.*" pageEncoding="UTF-8"%>
  <%
  String path = request.getContextPath();
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  %>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
  <head>
  <base href="<%=basePath%>">
  <title>HelloWord</title>
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->
  </head>
  <body>
  <%--
  <result name="list" type="redirect">不会显示${msg }的值
  <result name="list" type="dispatcher">会显示${msg }的值
  --%>
  ${msg }<br/>
  <%--获取url的请求参数 --%>
  <%--${param.username }会出现乱码 --%>
  ${param.username }<br/>
  <%=URLDecoder.decode(new String(request.getParameter("username").getBytes("ISO8859-1"),"UTF-8"),"UTF-8")%>
  </body>
  </html>
  helloworld_struts.xml子配置文件
  <?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "struts-2.0.dtd">
  <struts>
  <!-- 全局result:有很多时候一个result可供很多action使用,这时可以使用global-results标签来定义全局的result -->
  <package name="base" extends="struts-default">
  <global-results>
  <result name="msg">/message.jsp</result>
  </global-results>
  </package>
  <package name="helloWord" namespace="/helloWord" extends="base" >
  <!-- 访问路径: http://localhost:8083/struts2_1/helloWord/manage.do -->
  <action name="manage" class="com.ljq.action.HelloWordAction" method="msg"></action>
  <!-- 访问路径: http://localhost:8083/struts2_1/helloWord/listHelloWord.do
  http://localhost:8083/struts2_1/helloWord/msgHelloWord.do
  -->
  <action name="*HelloWord" class="com.ljq.action.HelloWordAction" method="{1}">
  <!-- 注意:在result中还可以使用${属性名}表达式访问action中的属性,表达式里的属性名对应action中的属性名 -->
  <result name="list" type="redirect">/index.jsp?username=${username}</result>
  </action>
  </package>
  </struts>
  struts.xml配置文件
  <?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "struts-2.0.dtd">
  <struts>
  <!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
  如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
  <constant name="struts.action.extension" value="do,php" />
  <!--解决乱码 -->
  <constant name="struts.i18n.encoding" value="UTF-8" />
  <!-- 设置浏览器是否缓存静态内容,默认值为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" />
  <!--<constant name="struts.objectFactory" value="spring" />-->
  <!-- 动态方法调用和使用通配符定义 -->
  <!--该属性设置Struts2是否支持动态方法调用,该属性的默认值为true。如果需要关闭动态方法调用,则可设置该为false -->
  <!--<constant name="struts.enable.DynamicMethodInvocation" value="false" />-->
  <!--上传文件的大小设置(这里的大小指的是总大小)-->
  <!--<constant name="struts.multipart.maxSize" value="10701096" />-->
  <!-- 引入 -->
  <include file="helloworld_struts.xml" />
  </struts>
  <!--注意:Struts2读取到Struts2.xml的内容后,以JavaBean形式存放在内存中。-->
  <!--以后Struts2对用户的每次请求处理将使用内存中的数据,而不是每次都读取Struts2.xml文件-->
  运行结果
  1、访问路径: http://localhost:8083/struts2_1/helloWord/manage.do
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值