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>
<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>
<bean class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"/>
</bean>
<context:component-scan base-package="com.zj"/>
<mvc:annotation-driven/>
<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>
<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"/>
<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>
<bean class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"/>
</bean>
<context:component-scan base-package="com.zj"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:default-servlet-handler/>
</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;
}
}