spring和springMVC集成javascript
在web.xml配置前端springMVC前端控制器(總控)html
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- 配置字符編碼過濾器 --> <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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置前端控制器 --> <servlet> <servlet-name>MVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- 讀取配置文件 問題:有多個spring相關配置文件如何讀取 如 :spring.xml 和springmvc.xml 解決方案: 方案一:直接使用 統配 * 能夠讀取多個有相同前綴的文件 <param-value>classpath:spring*.xml</param-value> 方案二:先讀取一個配置文件 若是 spring.xml 再在spring.xml文件中使用<import>標籤導入 springmvc.xml文件 <import resource="classpath:springmvc.xml"/> --> <param-value>classpath:spring.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MVC</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
springMVC.xml配置文件前端
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 配置springmvc的註解驅動 --> <mvc:annotation-driven/> </beans>
編寫表現層代碼(controller)java
@Controller @RequestMapping("/user") public class UserController { @Autowired private UserService service; @RequestMapping("/list") public String list(Model m) { //調用service查詢全部用戶方法 List<User> users = service.selectList(); //共享數據 m.addAttribute("users", users); return "/WEB-INF/view/user_list.jsp"; } @RequestMapping("/delete") public String delete(Integer id) { System.out.println(id); //調用service層的刪除方法 service.delteByPrimaryKey(id); return "redirect:/user/list.do"; } }
user_list.jsp頁面代碼web
在jsp頁面 使用jstl標籤庫須要先在頁面引入jstl 標籤庫spring
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!-- 引入jstl標籤庫 --> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h3>用戶列表</h3> <table border="1" style="width: 500px;" cellspacing="0"> <tr> <th>id</th> <th>名稱</th> <th>密碼</th> <th>年齡</th> <th>操做</th> </tr> <c:forEach items="${users}" var="user"> <tr> <td>${user.id}</td> <td>${user.name}</td> <td>${user.password}</td> <td>${user.age}</td> <td> <a href="javascript:void(0);" onclick="deleteByPrimaryKey(${user.id})">刪除</a> <a href="">修改</a> </td> </tr> </c:forEach> </table> <script type="text/javascript"> function deleteByPrimaryKey(userId){ if(confirm("親,您肯定刪除此條數據麼?")){ window.location.href= "${pageContext.request.contextPath}/user/delete.do?id="+userId; } } </script> </body> </html>