论坛首页 入门技术论坛

一种繁琐的SSH设计(二)

浏览 1570 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-04-10  
对于ACTION层的处理:
都继承了 LookupDispatchActionSupport ,利用ApplicationResources.properties 和 LookupMethods.properties来对应文字和方法(也就是按钮 和 按钮应该处理的方法)。
BaseAction.java
public class BaseAction extends LookupDispatchActionSupport {
	private static Log log = LogFactory.getLog(BaseAction.class);
	private static ApplicationContext ctx = null;
	private static String date_format = null;
	private static Long defaultLong = null;
	private static Map map = null;
        /**
	 * 获得对应关系的块
            */

	static {
		ResourceBundle bundle = ResourceBundle
				.getBundle("ApplicationResources");
		date_format = bundle.getString("date.format");
		ConvertUtils.register(new DateConverter(date_format), Date.class);
		ConvertUtils.register(new LongConverter(defaultLong), Long.class);
		ConvertUtils.register(new IntegerConverter(defaultLong), Integer.class);

		map = new HashMap();
		ResourceBundle methods = ResourceBundle.getBundle("LookupMethods");
		Enumeration keys = methods.getKeys();
		while (keys.hasMoreElements()) {
			String key = (String) keys.nextElement();
			map.put(key, methods.getString(key));
			log.info(key + "|" + methods.getString(key));
		}

	}

	protected Map getKeyMethodMap() {
		return map;
	}

	/**
	 * 根据spring配置的业务对象名称,取得业务对象实例
	 * 
	 * @param name
	 * @return
	 */
	public Object getBean(String name) {
		if (ctx == null) {
			ctx = ApplicationContextFactory.getApplicationContext();
		}
		return ctx.getBean(name);
	}
}


看一下文字对应的例子
这个例子中,将资源key和方法名称对放到资源文件LookupMethods.properties中。

资源文件LookupMethods.properties的内容如下:

button.edit=edit
button.delete=delete
......

然后,在struts的MessageResource使用的资源文件如 ApplicationResource.properties 中添加资源key的值:

button.edit=编辑
button.delete=删除
......

当然必须用ascii2native转换成unicode。
页面对应的处理
然后界面中就可以使用以下方式提交:

<html:submit property="method">
  <bean:message key="button.edit"/>
</html:submit>

或者

<html:submit property="method">
编辑
</html:submit>

Struts分发由struts-config.xml 和 url对应文件 以及validation.xml配置文件组成

struts-config.xml
  <form-beans>
		<form-bean name="userForm"
			type="com.bidlink.cms.forms.system.UserForm">
		</form-bean>
  <form-beans>
<action path="/userAction"
			type="com.bidlink.cms.actions.system.UserAction" name="userForm"
			parameter="method" input="addUser" unknown="false" scope="request">
			<forward name="upPass" path="user.upPass"></forward>
			<forward name="upUserInfo" path="user.upUserInfo"></forward>
			<forward name="searchLink" path="user.searchLink"></forward>
			<forward name="roleMana" path="user.roleMana"></forward>
		</action>


customerDefinitions.xml
	<definition name="user.upPass" path="/page/user/upPass.jsp" />
	<definition name="user.upUserInfo" path="/page/user/upUserInfo.jsp" />
	<definition name="user.readUserInfo" path="/page/user/readUserInfo.jsp" />


validation.xml
	<form name="userForm">
			<field property="username"
				depends="required,maxlength,minlength">
				<arg0 key="user.username" />
				<arg1 name="maxlength" key="${var:maxlength}"
					resource="false" />
				<arg1 name="minlength" key="${var:minlength}"
					resource="false" />
				<var>
					<var-name>maxlength</var-name>
					<var-value>20</var-value>
				</var>
				<var>
					<var-name>minlength</var-name>
					<var-value>2</var-value>
				</var>
			</field>
			<field property="password"
				depends="required,minlength,twofields">
				<arg0 key="user.password" />
				<arg1 name="minlength" key="${var:minlength}"
					resource="false" />
				<arg1 key="user.validatepassword" />
				<var>
					<var-name>minlength</var-name>
					<var-value>8</var-value>
				</var>
				<var>
					<var-name>secondProperty</var-name>
					<var-value>passwordRe</var-value>
				</var>
			</field>

			<field property="email" depends="required,email">
				<arg0 key="user.email" />
			</field>
			<field property="name" depends="required,maxlength">
				<arg0 key="user.name" />
				<arg1 name="maxlength" key="${var:maxlength}"
					resource="false" />
				<var>
					<var-name>maxlength</var-name>
					<var-value>4</var-value>
				</var>
			</field>
		</form>

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics