|
Struts 1.x
|
Struts 2.0
|
分类
|
将标志库按功能分成HTML、Tiles、Logic和Bean等几部分
|
严格上来说,没有分类,所有标志都在URI为“/struts-tags”命名空间 下,不过,我们可以从功能上将其分为两大类:非UI标志和UI标志
|
表达式语言(expression languages)
|
不支持嵌入语言(EL)
|
OGNL、JSTL、Groovy和Velcity
|
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" % > <%@ taglib prefix="s" uri="/struts-tags" % > <html> <head> <meta http-equiv="Content- Type" content="text/html; charset=GBK"/> <title>图书管 理系统</title> </head> <body> <p><a href="<s:url action="list" />">进入图书管理系统</a></p> </body> </html>
<%@page pageEncoding="gb2312" contentType="text/html; charset=UTF-8" % > <%@ taglib prefix="s" uri="/struts-tags" % > <html> <head><title>图书管 理系统</title></head> <style type="text/css"> table { border : 1px solid black; border-collapse : collapse; } table thead tr th { border : 1px solid black; padding : 3px; background-color : #cccccc; background-color : expression(this.rowIndex % 2 == 0 ? "#FFFFFF" : "#EEEEEE"); } table tbody tr td { border : 1px solid black; padding : 3px; } .trs { background-color : expression(this.rowIndex % 2 == 0 ? "#FFFFFF" : "#EEEEEE"); } </style> <script language="JavaScript"> function doSearch(){ if(document.all.searchValue.value=="" ) { alert("请输入查询关键字!" ); }else { window.location.href="bookAdmin/list.action?queryName="+document.all.searchName.value+"&&queryValue="+ document.all.searchValue.value; } } </script> <body> <table align="center"> <tr align="center"> <td> <select name="searchName"> <option value="bookName">书名</option> <option value="bookAuthor">作者</option> <option value="bookPublish">出版社</option> <option value="bookDate">出版日期</option> <option value="bookIsbn">ISNB</option> <option value="bookPage">页数</option> </select> <input type="text" name="searchValue" value="" size="10"/> <input type="button" value="查询" onClick="doSearch();"> </td> </tr> <tr align="center"> <td> <a href="<s:url action="list" includeParams="none"/>">全部</a> <a href='<s:url action="edit" ></s:url>'> 增加</a> </td> </tr> <tr> <td> <table cellspacing="0" align="center"> <thead> <tr> <th>书名</th> <th>作者</th> <th>出版社</th> <th>出版日期</th> <th>ISNB</th> <th>页数</th> <th>价格</th> <th>内容提要</th> <th>删除</th> </tr> </thead> <tbody> <s:iterator value="availableItems"> <tr class="trs"> <td> <a href='<s:url action="edit" ><s:param name="bookId" value="bookId" /></s:url> '> <s:property value="bookName"/> </a> </td> <td><s:property value="bookAuthor"/></td> <td><s:property value="bookPublish"/></td> <td><s:text name="format.date"><s:param value="bookDate"/></s:text></td> <td><s:property value="bookIsbn" /></td> <td><s:property value="bookPage" /></td> <td><s:property value="bookPrice"/></td> <td><s:property value="bookContent"/></td> <td><a href='<s:url action="delete"><s:param name="bookId" value="bookId" /></s:url>'>删除</a></td> </tr> </s:iterator> <tr align="right"> <td colspan="9"> 共 <s:property value="totalRows"/>行 第 <s:property value="currentPage"/>页 共 <s:property value="pager.getTotalPages()"/>页 <a href="<s:url value="list.action"> <s:param name="currentPage" value="currentPage"/> <s:param name="pagerMethod" value="'first'"/> </s:url>"> 首页</a> <a href="<s:url value="list.action"> <s:param name="currentPage" value="currentPage"/> <s:param name="pagerMethod" value="'previous'"/> </s:url>">上一页</a> <a href="<s:url value="list.action"> <s:param name="currentPage" value="currentPage"/> <s:param name="pagerMethod" value="'next'"/> </s:url>">下一页</a> <a href="<s:url value="list.action"> <s:param name="currentPage" value="currentPage"/> <s:param name="pagerMethod" value="'last'"/> </s:url>">尾页</a> </td> </tr> </tbody> </table> </td> </tr> </table> </body> </html>
名称
|
必需
|
默认
|
类型
|
描述
|
default
|
否
|
|
String
|
如果属性是null则显示的default值
|
escape
|
否
|
true
|
Booelean
|
是否escape HTML
|
value
|
否
|
栈顶
|
Object
|
要显示的值
|
id
|
否
|
|
Object/String
|
用来标识元素的id。在UI和表单中为HTML的id属性
|
名称
|
必需
|
默认
|
类型
|
描述
|
status
|
否
|
|
String
|
如果设置此参数,一个IteratorStatus的实例将会压入 每个遍历的堆栈
|
value
|
否
|
|
Object/String
|
要遍历的可枚举的(iteratable)数据源,或者将放入 新列表(List)的对象
|
id
|
否
|
|
Object/String
|
用来标识元素的id。在UI和表单中为HTML的id属性
|
(A)参数值会以String的格式放入statck.
(B)该值会以java.lang.Object的格式放入 statck.
名称
|
必需
|
默认
|
类型
|
描述
|
name
|
否
|
|
String
|
参数名
|
value
|
否
|
|
String
|
value表达式
|
id
|
否
|
|
Object/String
|
用来标识元素的id。在UI和表单中为HTML的id属性
|
![](http://cms.it168.com/fckeditor/editor/images/spacer.gif)
3、 增加/修改页面:editBook.jsp
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" % > <%@ taglib prefix="s" uri="/struts-tags" % > <html> <head> <title>编辑图书</title> <s:head/> </head> <body> <h2> <s:if test="null == book"> 增加图书 </s:if> <s:else> 编辑图书 </s:else> </h2> <s:form name="editForm" action="save" validate="true"> <s:textfield label="书名" name="book.bookName"/> <s:textfield label="作者" name="book.bookAuthor"/> <s:textfield label="出版社" name="book.bookPublish"/> <s:datetimepicker label="出版日期" name="book.bookDate"></s:datetimepicker> <s:textfield label="ISBN" name="book.bookIsbn"/> <s:textfield label="页数" name="book.bookPage"/> <s:textfield label="价格(元)" name="book.bookPrice"/> <s:textfield label="内容摘要" name="book.bookContent"/> <s:if test="null == book"> <s:hidden name="book.bookId" value="%{bookId}"/> </s:if> <s:else> <s:hidden name="book.bookId" /> </s:else> <s:hidden name="queryName" /> <s:hidden name="queryValue" /> <s:submit value="%{getText('保存')}" /> </s:form> <p><a href="<s:url action="list"/>">返回</a></p> </body> </html>
名称
|
必需
|
默认
|
类型
|
描述
|
备注
|
test
|
是
|
|
Boolean
|
决定标志里内容是否显示的表达式
|
else标志没有这个参数
|
id
|
否
|
|
Object/String
|
用来标识元素的id。在UI和表单中为HTML的id属性
|
|
名称
|
必需
|
默认
|
类型
|
描述
|
name
|
是
|
|
String
|
资源属性的名字
|
id
|
否
|
|
Object/String
|
用来标识元素的id。在UI和表单中为HTML的id属性
|
八、 配 置Struts2
Struts的配置文件都会在web.xml中注册的。
a) Struts的配置文件如下:
<? 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> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="GBK" /> <!-- Add packages here --> </struts> Src/struts.xml b) struts_book.xml配置文件如下: <? 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> <package name="products" extends="struts-default"> <!-- default-interceptor-ref name="validation"/ --> <!-- Add actions here -- > <action name="list" class="bookAction" method="list"> <result>/list.jsp</result> </action> <action name="delete" class="bookAction" method="delete"> <result type="redirect">list.action?queryMap=${queryMap} </result> </action> <action name="*" class="com.sterning.commons.AbstractAction"> <result>/{1}.jsp</result> </action> <action name="edit" class="bookAction" method="load"> <result>/editBook.jsp</result> </action> <action name="save" class="bookAction" method="save"> <interceptor-ref name="params"/> <interceptor-ref name="validation"/> <result name="input">/editBook.jsp</result> <result type="redirect">list.action?queryMap=${queryMap}</result> </action> </package> </struts>
文件中的<interceptor-ref name="params"/>,使 用 了struts2自己的拦截器,拦截器在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问 之 前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。
Struts 2已经提供了丰富多样的,功能齐全的拦截器实现 。大家可以到struts2-all -2.0.6.jar或struts2-core-2.0.6.jar包的struts-default.xml查看关于默认的拦 截器与拦截器链的配置。
在struts-default.xml中已经配置了大量的拦截器。如果 您想要使用这些已有的拦截器,只需要在应 用程序struts.xml文件中通过“<include file="struts- default.xml" />”将struts-default.xml文 件包含进来,并继承其中的struts-default包
(package), 最后在定义Action时,使用 “<interceptor-ref name="xx" />”引用拦截器或拦截器栈(interceptor stack)。一旦您继承了 struts-default包(package),所有Action都会调用拦截器栈 ——defaultStack。 当然,在Action配置中 加入“<interceptor-ref name="xx" />”可以覆盖defaultStack。
作为 “框架(framework)”,可扩展性是不可或缺的,因为世上没有放之四海而 皆准的东西。虽然, Struts 2为我们提供如此丰富的拦截器实现,但是这并不意味我们失去创建自定义拦截器的能力,恰恰相反, 在Struts 2自定义拦 截器是相当容易的一件事。所有的Struts 2的拦截器都直接或间接实现接口 com.opensymphony.xwork2.interceptor.Interceptor。 除此之外,大家可能更喜欢继承类 com.opensymphony.xwork2.interceptor.AbstractInterceptor。
九、 配置Spring
1、Spring的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- dataSource config --> <bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/game" /> <property name="username" value="root" /> <property name="password" value="root"/> </bean> <!-- SessionFactory --> <bean id="sessionFactory" class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="configLocation"> <value>classpath:com/sterning/bean/hibernate/hibernate.cfg.xml</value> </property> </bean> <!-- TransactionManager 不过这里暂时没注入--> <bean id="transactionManager" class ="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <!-- DAO --> <bean id="booksDao" class="com.sterning.books.dao.hibernate.BooksMapDao"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- Services --> <bean id="booksService" class="com.sterning.books.services.BooksService"> <property name="booksDao"> <ref bean="booksDao"/> </property> </bean> <bean id="pagerService" class="com.sterning.commons.PagerService"/> <!-- view --> <bean id="bookAction" class="com.sterning.books.web.actions.BooksAction" singleton="false"> <property name="booksService"> <ref bean="booksService"/> </property> <property name="pagerService"> <ref bean="pagerService"/> </property> </bean> </beans>
<?xml version="1.0" encoding="GB2312"?>
<! DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
![]()
<web-app>
<display-name>图书管理系统</display-name>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<!-- ContextConfigLocation -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context/applicationContext.xml</param-value>
</context-param>
![]()
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.sterning.commons.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,struts.xml,struts_books.xml</param-value>
</init-param>
</filter>
![]()
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
![]()
<!-- Listener contextConfigLocation -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Listener log4jConfigLocation -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
![]()
<!-- The Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
![](https://i-blog.csdnimg.cn/blog_migrate/a75f04c1f38fb30c6a47509ceac00d57.jpeg)
2、图书列表页面
![](https://i-blog.csdnimg.cn/blog_migrate/3d601a2452ae07d551bba768a237dded.jpeg)
3、增加页面
![](https://i-blog.csdnimg.cn/blog_migrate/3da0c9126febb66c8040397b9f6561b6.jpeg)
4、修改页面