Spring+Spring MVC+JDBC增删改查小例子
此篇文章,也是作为入门时的一个小练习例子,使用的是JDBC完成的增删改查,相对比较简单一点,只要Spirng MVC的控制器部分熟悉就可以拿下了,另外现在练手是基于xml的方式配置实体类的,增删改查是分别对应一个控制器,实际上是完全可以放在一个控制器里面实现的,像Struts一样,这个后面散仙会基于注解的方式给出例子。
web.xml里面的配置:
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <servlet> <servlet-name>qin</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置拦截 action为后缀名的请求 --> <servlet-mapping> <servlet-name>qin</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- HandlerMapping --> <bean value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 处理器 --> <bean name="/hellow" name="code">package com.qin.controller;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;import com.qin.dao.UserInfo;import com.qin.dao.impl.UserModelImpl;/** * 检索全部的控制器 * * **/public class QueryAllUserController extends AbstractController { public UserInfo dao=new UserModelImpl();@Overrideprotected ModelAndView handleRequestInternal(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception { ModelAndView mv=new ModelAndView();// mv.addObject("ulist", dao.getAllUser());arg0.setCharacterEncoding("UTF-8");arg1.setHeader("content-type", "text/html;charset=UTF-8"); Map<String, Object> maps = new HashMap<String, Object>(); //保存用户信息集合 maps.put("userInfoList", userInfoList); maps.put("clist", dao.getAllUser()); mv=new ModelAndView("/indexAll", maps); return mv; }}