开发第一个SPringMVC的程序

1.到如SPringMVC的jar包

2.配置web.xml文件(详情见SPringMVC的web.xml配置)

3.配置在classpath目录下的SPringMVC.xml文件


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 配置HandlerMapping处理器映射器对象 -->
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
	<!-- 配置HandlerAdapter处理器适配对象(适配器) -->
	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
	<!-- 配置视图解析 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/student/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 配置处理器(页面发送请求进入方法) -->
	<bean name="/student.action" class="com.mingde.controller.Studentlist"></bean>
	
</beans>
一开始执行服务端,然后在URL地址后面加上配置处理器的name,然后映射器去解析上面的URL地址,然后映射给配置处理器的class路径,找到那个studentlist.java文件,接着由适配器进入该studentlist.java文件执行里面的handleRequest方法,执行完后如果需要跳转到响应页面的话,那么会跳转到配置视图解析的指定位置,如/WEB-INF/student/XXX.JSP(XXX是指后台转过来的指定页面)

实体类和dao层在此省略……

controller中的Studentlist

package com.mingde.controlle;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.mingde.dao.StudentDao;
import com.mingde.dao.impl.StudentDaoImpl;
import com.mingde.po.Student;

public class Studentlist implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String cmd = request.getParameter("cmd");
		if("list".equals(cmd))return list();
		if("toAdd".equals(cmd))return new ModelAndView("toAdd");
		if("add".equals(cmd))return add(request,response);
		if("toUpdate".equals(cmd))return toUpdate(request,response);
		if("update".equals(cmd))return update(request,response);
		if("delete".equals(cmd))return delete(request,response);
		return null;
	}

	private ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sid = request.getParameter("sid");
		StudentDao sd=new StudentDaoImpl();
		sd.delete(sid);
		response.sendRedirect("student.action?cmd=list");
		return null;
	}

	private ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Student st=new Student();
		BeanUtils.populate(st, request.getParameterMap());
		StudentDao sd=new StudentDaoImpl();
		sd.update(st);
		response.sendRedirect("student.action?cmd=list");
		return null;
	}

	private ModelAndView toUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sid = request.getParameter("sid");
		StudentDao sd=new StudentDaoImpl();
		Student student = sd.find(sid);
		ModelAndView mv=new ModelAndView("update");
		mv.addObject("st", student);
		return mv;
	}

	private ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Student st=new Student();
		org.apache.commons.beanutils.BeanUtils.populate(st,request.getParameterMap());
		StudentDao sd=new StudentDaoImpl();
		sd.add(st);
		return list();
	}

	private ModelAndView list() {
		try {
			StudentDao sd=new StudentDaoImpl();
			List list;
			list = sd.SeeAll();
			ModelAndView mv=new ModelAndView("list");	//在构造是可以指定都跳转的视图:如要跳转到list
			mv.addObject("list", list);			//添加模型对象
			return mv;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
发布了62 篇原创文章 · 获赞 9 · 访问量 3万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章