好文档 - 专业文书写作范文服务资料分享网站

Struts2、Spring和Hibernate应用实例

天下 分享 时间: 加入收藏 我要投稿 点赞

class=\

classpath:com\\sterning\\bean\\hibernate\\hibernate.cfg.xml

class=\

WebRoot/WEB-INF/srping-content/applicationContent.xml 2、Struts.properties.xml

本来此文件应该写在struts 配置一节,但主要是考虑这体现了集成spring的配置,所以放在spring的配置这里来讲。

struts.objectFactory = spring struts.locale=zh_CN

struts.i18n.encoding = GBK struts.objectFacto:ObjectFactory 实现了 com.opensymphony.xwork2.ObjectFactory接口(spring)。struts.objectFactory=spring,主要是告知Struts 2运行时使用Spring来创建对象(如Action等)。当然,Spring的ContextLoaderListener监听器,会在web.xml文件中编写,负责Spring与Web容器交互。 struts.locale:The default locale for the Struts application。 默认的国际化地区信息。 struts.i18n.encoding:国际化信息内码。

十、Web.xml配置

PUBLIC \ \

图书管理系统

log4jConfigLocation

/WEB-INF/classes/log4j.properties

contextConfigLocation

/WEB-INF/spring-context/applicationContext.xml

encodingFilter

com.sterning.commons.SetCharacterEncodingFilter

encoding UTF-8

forceEncoding true

struts2

org.apache.struts2.dispatcher.FilterDispatcher

config

struts-default.xml,struts-plugin.xml,struts.xml,struts_books.xml

encodingFilter /*

struts2 /*

org.springframework.web.context.ContextLoaderListener

org.springframework.web.util.Log4jConfigListener

index.jsp

public void setQueryName(String queryName) { this.queryName = queryName; }

public String getQueryValue() { return queryValue; }

public void setQueryValue(String queryValue) { this.queryValue = queryValue; }

public String getSearchName() { return searchName; }

public void setSearchName(String searchName) { this.searchName = searchName; }

public String getSearchValue() { return searchValue; }

public void setSearchValue(String searchValue) { this.searchValue = searchValue;

}

public String getQueryMap() { return queryMap; }

public void setQueryMap(String queryMap) { this.queryMap = queryMap; }

public PagerService getPagerService() { return pagerService; }

public void setPagerService(PagerService pagerService) { this.pagerService = pagerService; } }

com.sterning.books.web.actions.BookAction.java

(1)、默认情况下,当请求bookAction.action发生时(这个会在后面的Spring配置文件中见到的),Struts运行时(Runtime)根据struts.xml里的Action映射集(Mapping),实例化com.sterning.books.web.actions.BookAction类,并调用其execute方法。当然,我们可以通过以下两种方法改变这种默认调用。这个功能(Feature)有点类似Struts 1.x中的LookupDispathAction。

在classes/sturts.xml中新建Action,并指明其调用的方法; 访问Action时,在Action名后加上“!xxx”(xxx为方法名)。

(2)、细心的朋友应该可能会发现com.sterning.books.web.actions.BookAction.java中Action方法(execute)返回都是SUCCESS。这个属性变量我并没有定义,所以大家应该会猜到它在ActionSupport或其父类中定义。没错,SUCCESS在接口com.opensymphony.xwork2.Action中定义,另外同时定义的还有ERROR, INPUT, LOGIN, NONE。

此外,我在配置Action时都没有为result定义名字(name),所以它们默认都为success。值得一提的是Struts 2.0中的result不仅仅是Struts 1.x中forward的别名,它可以实现除forward外的很激动人心的功能,如将Action输出到FreeMaker模板、Velocity模板、JasperReports和使用XSL转换等。这些都过result里的type(类型)属性(Attribute)定义的。另外,您还可以自定义result类型。

(3)、使用Struts 2.0,表单数据的输入将变得非常方便,和普通的POJO一样在Action编写Getter和Setter,然后在JSP的UI标志的name与其对应,在提交表单到Action时,我们就可以取得其值。

(4)、Struts 2.0更厉害的是支持更高级的POJO访问,如

this.getBook().getBookPrice()。private Books book所引用的是一个关于书的对象类,它可以做为一个属性而出现在BookActoin.java类中。这样对我们开发多层系统尤其有用。它可以使系统结构更清晰。 (5)、有朋友可能会这样问:“如果我要取得Servlet API中的一些对象,如request、response或session等,应该怎么做?这里的execute不像Struts 1.x的那样在参数中引入。”开发Web应用程序当然免不了跟这些对象打交道。在Strutx 2.0中可以有两种方式获得这些对象:非IoC(控制反转Inversion of Control)方式和IoC方式。

1s02r4yw276b8ve014gr
领取福利

微信扫码领取福利

微信扫码分享