開發第一個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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章