Posted on 2006-09-09 11:24
誰伴我闖蕩 閱讀(3181)
評論(0) 編輯 收藏
我曾經介紹過自己構建的分頁程序,在那個程序中,我們把分頁顯示的數據放到了Action的成員中,而沒有放在execute函數中,如果把他放到execute函數中會產生什么效果呢?由于我們沒有采用頁碼來確定數據,而是單純的調用下一頁來完成的,所以當變量放到execute中時我們每次訪問到的都只會是第一頁,這就是Struts的詬病,單用戶每次訪問都會為用戶創建一個新的線程,而非Servlet為每用戶單獨開辟一個線程,所以才會產生以上的問題?
那問題該如何解決呢?
1.用頁碼來區分需要顯示的數據更有效
2.Spring可以解決這個詬病
合二為一來介紹一下吧:
/**
?* Page.java
?* @author baputista
?* Email: baputista@hotmail.com
?* 2006-8-26 0:05:36
?*/
package subject.bean;
import java.util.ArrayList;
import java.util.List;
public class Page
{
?private int current = 1;?//當前頁碼
?private int nextPage = 0;?//下一頁碼
?private int previousPage = 0;//上一頁碼
?private int total = 0;//總的數據
?private int pages = 0;//總頁數
?private int each = 5;//每頁數據
?private int start = 0;//當前頁的起始數據
?private int end = 0;//當前頁的結束數據
?private boolean next = false;//是否存在下一頁
?private boolean previous = false;//是否存在上一頁
?private List list = null;
//需要顯示的數據
?public Page( List list, int each )//以需要顯示的數據和每頁要顯示的數據為參數進行初始化
?{
??this.list = list;
??this.each = each;
??total = list.size();//總的數據等于List的大小
??if ( total % each == 0 )//總頁數和沒頁顯示的數據可以整除,則為總頁數
???pages = total / each;
??else
???pages = total / each + 1;//否則就需要加上一頁了
??if ( current >= pages )
??{
???next = false;//判斷是否有下一頁
??}
??else
??{
???next = true;
???nextPage = current + 1;//有,還得算出來下一頁是多少呢!
??}
??if ( total < each )
??{
???start = 0;//這頁顯示多少數據啊?如果是最后一頁,只顯示整除后的余數了
???end = total;
??}
??else
??{
???start = 0;//否則要顯示每頁需要顯示的數據數
???end = each;
??}
?}
?public int getCurrent()
?{
??return current;
?}
?public boolean isNext()
?{
??return next;
?}
?public boolean isPrevious()
?{
??return previous;
?}
?public int getPages()
?{
??return pages;
?}
?public int getTotal()
?{
??return total;
?}
?public int getNextPage()
?{
??return nextPage;
?}
?public int getPreviousPage()
?{
??return previousPage;
?}
?@SuppressWarnings ( "unchecked" )
?public List get( int page )//獲取指定頁碼的List
?{
??if ( page > 0 && page <= pages )
??{
???current = page;//page在有效范圍內則為當前頁
??}
??if ( ( current - 1 ) > 0 )//計算上一頁是否存在以及值
??{
???previous = true;
???previousPage = current - 1;
??}
??else
??{
???previous = false;
??}
??if ( current >= pages )//計算下一頁是否存在以及值
??{
???next = false;
??}
??else
??{
???next = true;
???nextPage = current + 1;
??}
??if ( page * each < total )//計算顯示的記錄在List中的位置
??{
???end = current * each;
???start = end - each;
??}
??else
??{
???end = total;
???start = each * ( pages - 1 );
??}
??List gets = new ArrayList();//把顯示的數據放到一個新的List里
??for ( int i = start; i < end; i++ )
??{
???gets.add( list.get( i ) );
??}
??return gets;
?}
}
上面的Page Bean比那個就簡略的很多,我們再來看看Action怎么來做?
public class AdminAction extends BaseAction
{
?private AdminManager mgr;
?public void setAdminManager( AdminManager mgr )
?{
??this.mgr = mgr;
?}
public ActionForward execute( ActionMapping mapping, ActionForm form,
???HttpServletRequest request, HttpServletResponse response )
???throws Exception
?{
??Integer pageId = getInt( request, "page" );
??Page page = null;
??List rs =mgr.getStudents();
???if ( rs != null && rs.size() != 0 )
???{
????page = new Page( rs, 10 );
????request.setAttribute( "students", page.get( pageId ) );
???}
???request.setAttribute( "search", Select.studentSearch() );
??}??
??request.setAttribute( "page", page );
??return mapping.findForward("student");
?}
}
這樣我們就可以把分頁對象和List放到Execute中了,在這里我們把業務邏輯對象AdminManager的實例mgr聲明成了bean的形式,這樣可以在Spring中利用ioc來注入:
Struts的聲明:
<action path="/admin" type="org.springframework.web.struts.DelegatingActionProxy" />
Spring中的聲明:
<!-- 業務邏輯 -->
?<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
??<property name="transactionManager" ref="transactionManager" />
??<property name="transactionAttributes">
???<props>
????<prop key="save*">PROPAGATION_REQUIRED</prop>
????<prop key="remove*">PROPAGATION_REQUIRED</prop>
????<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
???</props>
??</property>
?</bean>
?<bean id="adminManager" parent="txProxyTemplate">
??<property name="target">
???<bean class="subject.service.impl.AdminManagerImpl">
????<property name="dao" ref="dao" />
???</bean>
??</property>
?</bean>
<bean name="/admin/admin" class="subject.web.action.AdminAction" singleton="false">
??<property name="adminManager">
???<ref bean="adminManager" />
??</property>
?</bean>
從而實現了Struts和Spring的完美暇接,singleton="false"從而可以解決單例的詬病!