`
heruito
  • 浏览: 16782 次
社区版块
存档分类
最新评论

spring 的MVC---转载

 
阅读更多

需要的包有:

commons-logging.jar 

log4j.jar  
spring2.jar 
cglib-nodep.jar 

1,web.xml 里面添加 DispatcherServlet 配置: 
<servlet> 
  <servlet-name>spring-mvc</servlet-name> 
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
  <init-param> 
   <param-name>contextConfigLocation</param-name> 
   <param-value>/WEB-INF/classes/spring-MVC.xml</param-value> 
</init-param> 
</servlet> 

<servlet-mapping> 
  <servlet-name>spring-mvc</servlet-name> 
  <url-pattern>*.jwml</url-pattern> 
</servlet-mapping> 




2,spring-MVC.xml 

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 

<beans> 
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > 
  <property name="mappings"> 
   <props> 
    <prop key="/bulletinList.jwml">bulletinListAction</prop> 
   </props> 
  </property> 
</bean> 
       
<bean id="bulletinListAction" class="com.eoms.wap.web.controller.BulletinListAction"> 
  <property name="pageSize"><value>3</value></property> 
  <property name="listJsp"><value>/bulletin/list/list.jsp</value></property> 
  <property name="errorJsp"><value>/error.jsp</value></property> 
</bean> 

</beans> 



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<BulletinInfo> 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; 


}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics