ycq__110 发表于 2013-2-6 10:07:58

struts2基础

Struts2 中访问servlet API的三种方式
1,间接访问put()/get()用来设置或者取得值。不能获得scope内置对象。
1)ActionContext.getContext.put(“”,””);
2)ActionContext.getContext.getSession.put(“”,””);
3)ActionContext.getContext.getApplication.put(“”,””);
2,servletActionContext.getResponse/getRequest()/
3,能直接获取servletApi实例。方法为实现下列接口。
servletContextAware   /    servletRequestAware    /    ServletResponseAware         /      SessionAware 此方法获得值类型Map<String,Object> Session
为了让BaseAction能够有验证的功能,并且不能被实例化,开发中我们这样做:
public abstract class BaseAction extends ActionSupport implements ServletResponseAware,
ServletRequestAware, SessionAware {};
然后我们在每个模块的Action来继承这个BaseAction类,然后我们就可以在Action中直接使用Servlet的API了。

Struts2动态调用三种方式:
1,指定method();
2,感叹号方式 action_name!result_name.拦截扩展名.
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
4,wildcard方式。Action_name=”chapter_*” action_method=”{1}”

模型驱动步骤
方法一:
模型驱动的action必须实现ModelDriven接口,而且要提供相应的泛型,这里就是具体使用的Model层的类,用于关联Action和Model.
实现的ModelDriven的getModel方法,其实就是简单返回泛型的一个对象。
在Action提供一个泛型的私有对象,这里就是定义一个User的user对象,并且提供相应的getter和setter。
注意:接口ModelDriven<user>必须实现getModel()方法。
Jsp中使用${user.userName};
方法二:
不需要实现的其它接口,我们只需改动Action和jsp即可。
注意:①Action类构造方法中可以不用实例化对象User;.
②<input type="text" name="user.userName"/>
   <input type="password" name="user.userPwd"/>

页面如何出现内置错误信息
<%@ taglib uri="/struts-tags" prefix="s"%>
<s:fielderror/>
自定义错误信息,需要用到properties属性文件,要求属性文件命名为ActionClassName.properties,并且将文件和Action类放在一个包下。
页: [1]
查看完整版本: struts2基础