Spring Servlet与SpringMVC控制层及其配置对比

Servlet xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <servlet>
    <servlet-name>empServlet</servlet-name>
    <servlet-class>com.zj.controller.empServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>empServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

Servlet

package com.zj.controller;

import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

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

import com.zj.entity.Emp;
import com.zj.service.EmpService;
import com.zj.service.impl.EmpServiceImpl;

@SuppressWarnings("serial")
public class empServlet extends HttpServlet {
	EmpService empService = new EmpServiceImpl();
	
	public void service(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, 
	IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;utf-8");
		String uri = request.getRequestURI();
		System.out.println(uri);
		String action=uri.substring(uri.lastIndexOf("/")+1,uri.lastIndexOf("."));
		System.out.println(action);
		
		if("list".equals(action)) {
			List<Emp> emps=empService.findAll();
			request.setAttribute("emps", emps);
			request.getRequestDispatcher("list.jsp").forward(request, response);
		}
		else if("toUpdate".equals(action)) {
			int empno=Integer.parseInt(request.getParameter("empno"));
			Emp emp =empService.findOne(empno);
			request.setAttribute("emp", emp);
			System.out.println(emp);
			request.getRequestDispatcher("update.jsp").forward(request, response);
		}
		else if("update".equals(action)) {
			String hiredate=request.getParameter("hiredate");
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			Emp emp=null;
			try {
				emp = new Emp(
						Integer.parseInt(request.getParameter("empno")),
						request.getParameter("ename"),
						Double.parseDouble(request.getParameter("salary")),
						Double.parseDouble(request.getParameter("bonus")),
						new Date(sdf.parse(hiredate).getTime()),
						Integer.parseInt(request.getParameter("deptno")));
			} catch (Exception e) {
				e.printStackTrace();
			}
			empService.updateOne(emp);
			response.sendRedirect("list.do");
		}
		else if("toAdd".equals(action)) {
			request.getRequestDispatcher("add.jsp").forward(request, response);
		}
		//添加控件
		else if("add".equals(action)) {
			String hiredate=request.getParameter("hiredate");
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			Emp emp=null;
			try {
				emp = new Emp(0,
						request.getParameter("ename"),
						Double.parseDouble(request.getParameter("salary")),
						Double.parseDouble(request.getParameter("bonus")),
						new Date(sdf.parse(hiredate).getTime()),
						Integer.parseInt(request.getParameter("deptno")));
			} catch (Exception e) {
				e.printStackTrace();
			}
			empService.addOne(emp);
			response.sendRedirect("list.do");
		}
		else if("delete".equals(action)) {
			int empno= Integer.parseInt(request.getParameter("empno"));
			empService.deletById(empno);
			response.sendRedirect("list.do");
		}
	}
}

SpringMVC xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>EmpV4</display-name>
  <!-- 配置dispatcherServlet -->
  <servlet>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:spring.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <!-- 中文乱码 -->
  <filter>
  	<filter-name>encodingFilter</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>
  </filter>
  <filter-mapping>
  	<filter-name>encodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
	
	<!-- 配置数据库 -->
	<util:properties id="db" location="classpath:db.properties"/>
	
	<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="#{db.driver}"/>
		<property name="url" value="#{db.url}"/>
		<property name="username" value="#{db.user}"/>
		<property name="password" value="#{db.pwd}"/>
	</bean>
	
	<!-- 声明jdbcTemplate -->
	<bean class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="ds"/>
	</bean>
	
	<!-- 声明注解 -->
	<context:component-scan base-package="com.zj"/>
	
	<!-- spring mvc注解 -->
	<mvc:annotation-driven/>
	
	<!-- spring mvc声明视图 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"/>
		<property name="suffix" value=".jsp"/>
	</bean>

</beans>

SpringMVC

package com.zj.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zj.entity.Emp;
import com.zj.service.EmpService;

@Controller
@RequestMapping("emp")
public class EmpController {
	@Autowired
	private EmpService empService;
	
	@RequestMapping("list.do")
	public String findAll(HttpServletRequest request) {
		List<Emp> emps = empService.findAll();
		request.setAttribute("emps", emps);
		return "list";
	}
	
	@RequestMapping("toAdd.do")
	public String toAdd() {
		return "add";
	}
	
	@RequestMapping("add.do")
	public String addEmp(Emp emp) {
		empService.addEmp(emp);
		return "redirect:/emp/list.do";
	}
	
	@RequestMapping("toUpdate.do")
	public String toUpdate(HttpServletRequest request,int empno) {
		Emp emp = empService.findByEmpno(empno);
		request.setAttribute("emp", emp);
		return "update";
	}
	
	@RequestMapping("update.do")
	public String updateEmp(Emp emp) {
		System.out.println(emp);
		empService.updateEmp(emp);
		return "redirect:/emp/list.do";
	}
	
	@RequestMapping("delete.do")
	public String deleteEmp(int empno) {
		empService.deleteEmp(empno);
		return "redirect:/emp/list.do";
	}
}

RestfulSpringMVC xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>EmpV4</display-name>
  <!-- 配置dispatcherServlet -->
  <servlet>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:spring.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 中文乱码 -->
  <filter>
  	<filter-name>encodingFilter</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>
  </filter>
  <filter-mapping>
  	<filter-name>encodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
	
	<!-- 配置数据库 -->
	<util:properties id="db" location="classpath:db.properties"/>
	
	<!-- 声明BasicDataSource -->
	<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="#{db.driver}"/>
		<property name="url" value="#{db.url}"/>
		<property name="username" value="#{db.user}"/>
		<property name="password" value="#{db.pwd}"/>
	</bean>
	
	<!-- 声明jdbcTemplate -->
	<bean class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="ds"/>
	</bean>
	
	<!-- 声明IOC注解扫描 -->
	<context:component-scan base-package="com.zj"/>
	
	<!-- springMVC注解扫描 -->
	<mvc:annotation-driven/>
	
	<!-- springMVC视图声明 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
	<!-- 支持RESTful访问静态资源 -->
	<mvc:default-servlet-handler/>
	
	<!-- <mvc:resources location="" mapping=""/> -->

</beans>

RestfulSpringMVC

package com.zj.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zj.entity.Emp;
import com.zj.service.EmpService;

@Controller
@RequestMapping("/")
public class EmpController {
	@Autowired
	private EmpService empService;
	
	@RequestMapping(value="emps",method=RequestMethod.GET)
	public String findAll(HttpServletRequest request) {
		List<Emp> emps = empService.findAll();
		request.setAttribute("emps", emps);
		return "emp/emp_list";
	}
	
	@RequestMapping(value="emp",method = RequestMethod.GET)
	public String toAdd() {
		return "emp/emp_add";
	}
	
	@RequestMapping(value="emp",method = RequestMethod.POST)
	public String addEmp(Emp emp) {
		empService.addEmp(emp);
		return "redirect:emps";
	}
	
	@RequestMapping(value="emp/{id}",method = RequestMethod.GET)
	public String toUpdate(Model model,
			@PathVariable("id") int empno) {
		Emp emp = empService.findByEmpno(empno);
		model.addAttribute("emp", emp);
		return "emp/emp_update";
	}
	
	@RequestMapping(value="emp",method = RequestMethod.PUT)
	@ResponseBody
	public boolean updateEmp(@RequestBody Emp emp) {
		System.out.println(emp);
		empService.updateEmp(emp);
		return true;
	}
	
	@RequestMapping(value="emp/{id}",method = RequestMethod.DELETE)
	@ResponseBody
	public boolean deleteEmp(@PathVariable("id") int empno) {
		empService.deleteEmp(empno);
		return true;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BirdMan98

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值