标签:idt lang rtu SSM框架 private 密码 dir 集成 自己
spring和springMVC集成
在web.xml配置前端springMVC前端控制器(总控)
<?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)
@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页面代码
在jsp页面 使用jstl标签库需要先在页面引入jstl 标签库
<%@ 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>
标签:idt lang rtu SSM框架 private 密码 dir 集成 自己
原文地址:https://www.cnblogs.com/zhangxiong-tianxiadiyi/p/11172184.html