大家都知道Struts是一種基于MVC的結(jié)構(gòu),而這個(gè)MVC又怎么樣理解呢?書上闡述的一般都很詳細(xì),而我的理解很直白,我們可以把業(yè)務(wù)邏輯放到每個(gè)JSP頁(yè)面中,當(dāng)你訪問(wèn)一個(gè)JSP頁(yè)面的時(shí)候,就可以看到業(yè)務(wù)邏輯得到的結(jié)果,而把這些業(yè)務(wù)邏輯與HTML代碼夾雜到了一起,一定會(huì)造成一些不必要的麻煩,可以不可以不讓我們的業(yè)務(wù)邏輯和那些HTML代碼夾雜到一起呢?多少得攙雜一些,那干脆,盡量少的吧,于是我們可以嘗試著把業(yè)務(wù)邏輯的運(yùn)算過(guò)程放到一個(gè)Action里,我們?cè)L問(wèn)這個(gè)Action,之后Action執(zhí)行業(yè)務(wù)邏輯,最后把業(yè)務(wù)邏輯的結(jié)果放到request中,并將頁(yè)面請(qǐng)求轉(zhuǎn)發(fā)給一個(gè)用于顯示結(jié)果的jsp頁(yè)面,這樣,這個(gè)頁(yè)面就可以少去很多的業(yè)務(wù)邏輯,而只是單純的去顯示一些業(yè)務(wù)邏輯計(jì)算結(jié)果的頁(yè)面而已。這時(shí)的Action稱為控制器,JSP頁(yè)可以叫做視圖了,而控制器操作的業(yè)務(wù)對(duì)象,無(wú)非就應(yīng)該叫模型了!
從上面的話,我們來(lái)分析一下當(dāng)我們要做一個(gè)分頁(yè)時(shí)所需要的部分,而在這之前,我們先看看他們的執(zhí)行過(guò)程吧,首先我們第一次請(qǐng)求訪問(wèn)一個(gè)頁(yè)面,它會(huì)把所有記錄的前N條顯示給我們,之后計(jì)算是否有下一頁(yè),等類似的信息,當(dāng)我們點(diǎn)下一頁(yè)的時(shí)候,就獲取下一頁(yè)的信息,我們還可以添加一個(gè)搜索,比如我們用于顯示學(xué)生的,可以安學(xué)生姓名查找,學(xué)號(hào)查找,班級(jí)查找。而對(duì)于顯示的對(duì)象,我們一般也都會(huì)封裝為javabean,所以用于放置查詢結(jié)果的容器是不定的,而這時(shí),我們就需要用泛型來(lái)提升我們的代碼效率!
首先我們寫一個(gè)用于分頁(yè)顯示的javabean:
package com.boya.subject.model;
import java.util.Vector;
public class Page<E>
{
??? private int current = 1;??????? //當(dāng)前頁(yè)
??? private int total = 0;???????? //總記錄數(shù)
??? private int pages = 0;??? //總頁(yè)數(shù)
??? private int each = 5;???????? //每頁(yè)顯示
??? private int start = 0;????? //每頁(yè)顯示的開始記錄數(shù)
??? private int end = 0;?????? //每頁(yè)顯示的結(jié)束記錄數(shù)
??? private boolean next = false;??????? //是否有下一頁(yè)
??? private boolean previous = false;? //是否有上一頁(yè)
??? private Vector<E> v = null;???
//存放查詢結(jié)果的容器
??? public Page( Vector<E> v ,int per)
??? {
??????? this.v = v;
??????? each = per;
??????? total = v.size();?? //容器的大小就是總的記錄數(shù)
??????? if ( total % each == 0 )
??????????? pages = total / each;?????? //計(jì)算總頁(yè)數(shù)
??????? else
??????????? pages = total / each + 1;
??????? if ( current >= pages )
??????? {
??????????? next = false;
??????? }
??????? else
??????? {
??????????? next = true;
??????? }
??????? if ( total < each )
??????? {
??????????? start = 0;
??????????? end = total;
??????? }
??????? else
??????? {
??????????? start = 0;
??????????? end = each;
??????? }
??? }
???
??? public int getCurrent()
??? {
??????? return current;
??? }
??? public void setCurrent( int current )
??? {
??????? this.current = current;
??? }
??? public int getEach()
??? {
??????? return each;
??? }
??? public void setEach( int each )
??? {
??????? this.each = each;
??? }
??? public boolean isNext()
??? {
??????? return next;
??? }
??? public void setNext( boolean next )
??? {
??????? this.next = next;
??? }
??? public boolean isPrevious()
??? {
??????? return previous;
??? }
??? public void setPrevious( boolean previous )
??? {
??????? this.previous = previous;
??? }
??? public int getEnd()
??? {
??????? return end;
??? }
??? public int getPages()
??? {
??????? return pages;
??? }
??? public int getStart()
??? {
??????? return start;
??? }
??? public int getTotal()
??? {
??????? return total;
??? }
?//獲取下一頁(yè)的對(duì)象們??
public Vector<E> getNextPage()
??? {
??????? current = current + 1;
??????? if ( (current - 1) > 0 )
??????? {
??????????? previous = true;
??????? }
??????? else
??????? {
??????????? previous = false;
??????? }
??????? if ( current >= pages )
??????? {
??????????? next = false;
??????? }
??????? else
??????? {
??????????? next = true;
??????? }
??????? Vector<E> os = gets();
??????? return os;
??? }
?//獲取上一頁(yè)
??? public Vector<E> getPreviouspage()
??? {
??????? current = current - 1;
??????? if ( current == 0 )
??????? {
??????????? current = 1;
??????? }
??????? if ( current >= pages )
??????? {
??????????? next = false;
??????? }
??????? else
??????? {
??????????? next = true;
??????? }
??????? if ( (current - 1) > 0 )
??????? {
??????????? previous = true;
??????? }
??????? else
??????? {
??????????? previous = false;
??????? }
??????? Vector<E> os = gets();
??????? return os;
??? }
?//一開始獲取的
??? public Vector<E> gets()
??? {
??????? if ( current * each < total )
??????? {
??????????? end = current * each;
??????????? start = end - each;
??????? }
??????? else
??????? {
??????????? end = total;
??????????? start = each * (pages - 1);
??????? }
??????? Vector<E> gets = new Vector<E>();
??????? for ( int i = start; i < end; i++ )
??????? {
??????????? E o = v.get( i );
??????????? gets.add( o );
??????? }
??????? return gets;
??? }
}
?而對(duì)于按不同搜索,我們需要一個(gè)FormBean,一般的搜索,都是模糊搜索,搜索個(gè)大概,而且輸入的信息中文的比重也會(huì)很大,所以,我把對(duì)中文字符的轉(zhuǎn)換放到了這個(gè)BEAN里,在進(jìn)行select * from * where like這樣的查詢時(shí),如果是like ''這樣就可以得到所有的記錄了,我便用這個(gè)來(lái)對(duì)付沒(méi)有輸入查詢關(guān)鍵字的情況,而like '%*%'可以匹配關(guān)鍵字,而%%也在這里添加上了!
package com.boya.subject.view;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class SearchForm extends ActionForm
{
??? private static final long serialVersionUID = 1L;
??? private String key;
??? private String from;
??? public String getFrom()
??? {
??????? return from;
??? }
??? public void setFrom( String from )
??? {
??????? this.from = from;
??? }
???
? public void reset( ActionMapping mapping, HttpServletRequest req )
??? {
??????? this.key = null;
??? }
??? public String getKey()
??? {
??????? return key;
??? }
??? public void setKey( String key )
??? {
??????? try
??????? {
??????????? key = new String( key.getBytes( "iso-8859-1" ), "gb2312" );
??????? }
??????? catch ( UnsupportedEncodingException e )
??????? {
??????????? e.printStackTrace();
??????? }
??????? this.key = "%" + key + "%";
??? }
???
??? public String getAny(){
??????? return "%%";
??? }
}
前期都做好了,我現(xiàn)在就要開始訪問(wèn)這個(gè)Action了,可是這個(gè)控制器還沒(méi)寫呢!這里是代碼
public class AdminUserAction extends AdminAction
{
??? private Vector<Student> ss; //用來(lái)裝結(jié)果的容器
??? private Page<Student> ps;
//分頁(yè)顯示的PAGE對(duì)象
??? protected ActionForward executeAction( ActionMapping mapping,
??????????? ActionForm form, HttpServletRequest req, HttpServletResponse res )
??????????? throws Exception
??? {
??????? if ( !isSupper( req ) )
??????? {
??????????? return notSupper( res );//如果不是超級(jí)管理員怎么辦?
??????? }
??????? Service service = getService();//獲取業(yè)務(wù)邏輯
??????? SearchForm sf = (SearchForm) form;//獲取搜索FORM
??????? String op = req.getParameter( "op" );//獲取用戶對(duì)頁(yè)面的操作
??????? String search = req.getParameter( "search" );//是否執(zhí)行了搜索
??????? Vector<Student> temp = null;?//用于存放臨時(shí)反饋給用戶的結(jié)果容器
??????????????? if ( op == null )//如果用戶沒(méi)有執(zhí)行上/下一頁(yè)的操作
??????????????? {
??????????????????? if ( search != null )//用戶如果執(zhí)行了搜索
??????????????????? {
??????????????????????? if ( sf.getFrom().equalsIgnoreCase( "class" ) )//如果是按班級(jí)查找
??????????????????????? {
??????????????????????????? ss = service.getAllStudentBySchoolClassForAdmin( sf
??????????????????????????????????? .getKey() );//獲取from的關(guān)鍵字
??????????????????????? }
??????????????????????? else if ( sf.getFrom().equalsIgnoreCase( "name" ) )//如果是按姓名查找
??????????????????????? {
??????????????????????????? ss = service.getAllStudentByNameForAdmin( sf
??????????????????????????????????? .getKey() );
??????????????????????? }
??????????????????????? else if ( sf.getFrom().equalsIgnoreCase( "user" ) )//如果是按用戶名查找
??????????????????????? {
??????????????????????????? ss = service.getAllStudentByUserForAdmin( sf
??????????????????????????????????? .getKey() );
??????????????????????? }
??????????????????????? else
??????????????????????? {
??????????????????????????? ss = service.getAllStudentBySnForAdmin( sf.getKey() );//按學(xué)號(hào)查找
??????????????????????? }
??????????????????????? form.reset( mapping, req );//重置搜索表單
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? ss = service.getAllStudentForAdmin( sf.getAny() ); //用戶未執(zhí)行查找就顯示全部,
??????????????????? }
??????????????????? if ( ss != null && ss.size() != 0 )//如果查找不為空,有記錄,那就創(chuàng)建一個(gè)分頁(yè)對(duì)象
??????????????????? {
??????????????????????? ps = new Page<Student>( ss, 10 );//將查詢結(jié)果和每頁(yè)顯示記錄數(shù)作為參數(shù)構(gòu)件對(duì)象
??????????????????????? temp = ps.gets();//并獲取第一頁(yè)
??????????????????? }
??????????????? }
??????????????? else//如果用戶執(zhí)行了操作
??????????????? {
??????????????????? if ( op.equals( "next" ) )//操作是下一頁(yè)
??????????????????? {
??????????????????????? temp = ps.getNextPage();
??????????????????? }
??????????????????? if ( op.equals( "previous" ) )//操作是上一頁(yè)
??????????????????? {
??????????????????????? temp = ps.getPreviouspage();
??????????????????? }
??????????????? }
??????????????? req.setAttribute( "search", SelectUtil.studentSearch() );//把搜索用到的表單放到request中
??????????????? req.setAttribute( "students", temp );//該頁(yè)顯示的學(xué)生
??????????????? req.setAttribute( "page", ps );//分頁(yè)對(duì)象
??????????????? return mapping.findForward( "student" );//請(qǐng)求轉(zhuǎn)發(fā)
??? }
}
用到SelectUtil中的代碼如下:
/**
???? * 獲取學(xué)生查找類別的select
???? * @return 學(xué)生查找類別
???? * 2006-5-17 9:06:12
???? */
??? public static Vector<LabelValueBean> studentSearch()
??? {
??????? Vector<LabelValueBean> s = new Vector<LabelValueBean>();
??????? s.add( new LabelValueBean( "按學(xué)號(hào)查找", "sn" ) );
??????? s.add( new LabelValueBean( "按班級(jí)查找", "class" ) );
??????? s.add( new LabelValueBean( "按姓名查找", "name" ) );
??????? s.add( new LabelValueBean( "按用戶查找", "user" ) );
??????? return s;
??? }
在看頁(yè)面視圖前先讓我們看看Model吧,
public class Student extends User
{
??? private String sn;
??? private SchoolClass schoolClass;
//這里的班級(jí)做為了一種對(duì)象,我們?cè)谝晥D顯示的時(shí)候就有了一層嵌套
??? public SchoolClass getSchoolClass()
??? {
??????? return schoolClass;
??? }
??? public void setSchoolClass( SchoolClass schoolClass )
??? {
??????? this.schoolClass = schoolClass;
??? }
??? public String getSn()
??? {
??????? return sn;
??? }
??? public void setSn( String sn )
??? {
??????? this.sn = sn;
??? }
??? public String getType()
??? {
??????? return "student";
??? }
}
在了解了model后,還是看看視圖吧,
先放個(gè)查詢表單:
<html:javascript dynamicJavascript="true" staticJavascript="true" formName="SearchForm" />
<html:form action="/adminUser.do?search=true" onsubmit="return validateSearchForm(this)">
<html:select property="from" >
<html:options collection="search" property="value" labelProperty="label" />
</html:select>
<html:text property="key" size="16" maxlength="16"/>
<html:image src="images/search.gif"/>
</html:form>
由于模型中有嵌套,那么我們就將用到Nested標(biāo)簽,其實(shí)沒(méi)有嵌套也可以使用這個(gè)標(biāo)簽,下面的是用于顯示信息的,用迭迨器進(jìn)行遍歷request范圍的students,你不安排范圍,他會(huì)自動(dòng)找到的,并把每次遍歷的對(duì)象起名叫student,并作為層次的根元素,
<logic:iterate id="student" name="students">
<nested:root name="student">
<nested:nest property="schoolClass"><nested:write property="schoolClass"/></nested:nest>//尋找了student的schoolClass屬性對(duì)象的schoolClass嵌套
<nested:write property="name"/>????? //student的名字
<nested:link page="/adminActions.do?method=deleteStudent" paramId="id" paramProperty="id" onclick="return window.confirm('您真的要?jiǎng)h除嗎?')">刪除</nested:link>
</nested:root>
這里是顯示分頁(yè)對(duì)象的:
第<bean:write name="page" property="current" />頁(yè)
共<bean:write name="page" property="pages" />頁(yè)
??????? //上一頁(yè)是否存在
????????<logic:equal name="page" property="previous" value="true">
???????????????<html:link page="/adminUser.do?part=student&op=previous">
????????????????<font color="6795b4">上一頁(yè)</font>
???????????????</html:link> ?
??????? </logic:equal>
????????<logic:notEqual name="page" property="previous" value="true">上一頁(yè) ? </logic:notEqual>
????????
?????? //下一頁(yè)是否存在
?????????<logic:equal name="page" property="next" value="true">
?????????<html:link page="/adminUser.do?part=student&op=next">
??????????<font color="6795b4">下一頁(yè)</font>
?????????</html:link> ? </logic:equal>
????????<logic:notEqual name="page" property="next" value="true">下一頁(yè) ? </logic:notEqual>
????????
共有<bean:write name="page" property="total" />條數(shù)據(jù)
</logic:iterate>
到這里不知道您看明白了多少,在我的這個(gè)JSP頁(yè)里幾乎沒(méi)有任何的業(yè)務(wù)邏輯,這樣的設(shè)計(jì)就比把HTML和JAVA攙雜在一起好了很多。