spring MVC

需要的JAR包有:
spring2.jar
cglib-nodep.jar
commons-logging.jar
log4j.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;
}

}


官方例子也有。
下面是MVC-Basic和MVC-Ajax的代码。
直接用Subversion客户端下载、Maven编译即可。

https://src.springframework.org/svn/spring-samples/mvc-basic/trunk/
https://src.springframework.org/svn/spring-samples/mvc-ajax/trunk/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值