struts2.X心得6--动态方法、通配符、常量案例分析

1.常量、全局视图的案例分析,主要内容就是在配置文件中添加的常量或全局视图标签以及讲struts.xml参数带过去
外部引进的配置文件struts-constant.xml
<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<!-- 常用的常量介绍,放在struts.xml中struts标签的最上面 -->


	<!--
		该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
		如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。
	-->
	<constant name="struts.action.extension" value="action,do"></constant>

	<!--
		指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法 和freemarker
		、velocity的输出 ; 默认的是UTF-8,其他字符集会中文乱码
	-->
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>

	<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
	<constant name="struts.configuration.xml.reload" value="true"></constant>

	<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
	<constant name="struts.serve.static.browserCache" value="false" />

	<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
	<constant name="struts.devMode" value="true" />


	<!-- 默认的视图主题 -->
	<constant name="struts.ui.theme" value="simple" />


	<!-- 同一个项目的相同目录下的配置文件中的包名如果已经被 其中一个配置文件占用,那么其他配置文件则不能再用这个名字-->
	<package name="admin" namespace="/csdn" extends="struts-default">
		<action name="login" class="www.csdn.action.LoginAction"
			method="login">
			<result>/sc.jsp</result>
		</action>
	</package>

</struts>

Struts.xml
<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

<!-- 将外面的xml配置文件包含进来,让struts自动读取 -->
<include file="struts-constant.xml"/>


<package name="user" namespace="/csdn" extends="struts-default">
  
  <!--  全局的结果集    多个action可以共用这一个结果集中跳转的页面    -->
  <global-results>
    <result name="success">/sc.jsp</result>
  </global-results>
 
  
  <action name="login_error" class="www.csdn.action.LoginAction" method="login">
    <!-- 除了可以从页面直接传过来值之外,action中的param标签也可以进行传值 ,但是在相应的action类中有对象的属性即set构造器-->
    <param name="test">/as....</param>
    <!--<result>/sc.jsp</result> -->
  </action>
  </package>
</struts>

2.通配符项目应用案例分析,这里只是简单列出struts.xml文件和以前项目的不同之处,本案例是在project项目(与hibernate整合,本人struts2博客的前几篇有分析项目案例)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

	<package name="test" namespace="/csdn" extends="struts-default">
	
		<action name="adminAction_*" class="www.csdn.action.AdminAction"
			method="{1}">
			<result name="success" type="redirectAction">adminAction_adminList.action?adminName=${
				entity.adminName }</result>
			<result name="login">/index.jsp</result>
			<result name="find">/find.jsp</result>
			<result name="list">/list.jsp</result>
		</action>

	</package>

</struts>


Jsp显示页面的不同之处就是:
<form
				action="${pageContext.request.contextPath}/csdn/adminAction_adminLogin.action"
				method="post">
      分析:以login登录为例,就是以前叫做adminLogin;现在需要改成adminAction_adminLogin;因为struts配置文件中将action标签中的name值改成了adminAction_*;依次类推,改项目的所有form跳转页面的地址和html超链接跳转的地址都要改成adminAction_加以前action的名字。

Action的不同之处:
	// 登录方法
	public String adminLogin() {
		entity = service.login(adminName, adminPassword);
		if (entity != null) {
			return SUCCESS;
		} else {
			return LOGIN;
		}
	}
分析:以login登录为例,这里的方法名字现在要统一起来,就是说jsp中的adminAction_adminLogin名字下划线后面的adminLogin要作为action类的方法,增删改查方法都要这么做。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值