spring 的MVC

阅读更多

需要的包有:

commons-logging.jar 

log4j.jar  
spring2.jar 
cglib-nodep.jar 

1,web.xml 里面添加 DispatcherServlet 配置: 
 
  spring-mvc 
  org.springframework.web.servlet.DispatcherServlet 
   
   contextConfigLocation 
   /WEB-INF/classes/spring-MVC.xml 
 
 

 
  spring-mvc 
  *.jwml 
 




2,spring-MVC.xml 

 
 

 
 
   
    
    bulletinListAction 
    
   
 
       
 
  3 
  /bulletin/list/list.jsp 
  /error.jsp 
 

 



3,   action的编写。 

BulletinListAction.java 
package com.eoms.wap.web.controller; 
import java.util.List; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.Controller; 
import com.eoms.wap.entity.BulletinInfo; 
import com.eoms.wap.factory.SpringFactory; 
import com.eoms.wap.mgr.BulletinMgr; 

/** 
* 公告列表页Action 
* @author wangyudong 

*/ 
public class BulletinListAction implements Controller{ 

private int pageSize = 10 ; 

private String listJsp = null ; 

private String errorJsp = null ; 


private BulletinMgr bulletinMgr = (BulletinMgr)SpringFactory.getBean("bulletinMgr"); 

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { 
  
  int pageInt = 1 ; 
  String page = request.getParameter("page"); 
  
  if(page!=null){ 
   try{ 
    pageInt=Integer.parseInt(page); 
   }catch(Exception ex){ 
    pageInt=1; 
   } 
  } 
  
  int start = (pageInt-1)*pageSize; 
  int max = pageSize ; 
  List list = bulletinMgr.findBulletinInfoListByStatus(BulletinInfo.STATUS_HAS_AUDIT, start, max); 
  int total = bulletinMgr.findTotalCountByStatus(BulletinInfo.STATUS_HAS_AUDIT); 
  
  if(list==null || list.size()==0){ 
   request.setAttribute("error", "没有找到可展示公告"); 
   return new ModelAndView(errorJsp); 
  } 
  boolean hasPrev = false; 
  boolean hasNext = false; 
  
  if(pageInt>1){ 
   hasPrev = true ; 
  } 
  if( pageInt*pageSize < total ){ 
   hasNext = true ; 
  } 
  
  request.setAttribute("list", list ); 
  request.setAttribute("page", new Integer(pageInt)); 
  request.setAttribute("total", new Integer(total)); 
  request.setAttribute("hasPrev", new Boolean(hasPrev) ); 
  request.setAttribute("hasNext", new Boolean(hasNext) ); 
  
  return new ModelAndView(listJsp); 



// getter and setter // 
public String getListJsp() { 
  return listJsp; 


public void setListJsp(String listJsp) { 
  this.listJsp = listJsp; 


public String getErrorJsp() { 
  return errorJsp; 


public void setErrorJsp(String errorJsp) { 
  this.errorJsp = errorJsp; 


public int getPageSize() { 
  return pageSize; 


public void setPageSize(int pageSize) { 
  this.pageSize = pageSize; 


}

 

你可能感兴趣的:(spring 的MVC)