微服务 教程
1085个小节阅读:196.1k
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
在父工程下使用maven创建web类型子工程
controller工程引入service子工程的依赖,并配置ssm父工程
xxxxxxxxxx
<parent>
<artifactId>ssm_demo</artifactId>
<groupId>com.itbaizhan</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.itbaizhan</groupId>
<artifactId>ssm_service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
编写控制器类
xxxxxxxxxx
"/student") (
public class StudentController {
private StudentService studentService;
"/all") (
public String all(Model model){
List<Student> allStudent = studentService.findAllStudent();
model.addAttribute("students",allStudent);
return "allStudent";
}
"/add") (
public String add(Student student){
studentService.addStudent(student);
// 重定向到查询所有学生
return "redirect:/student/all";
}
}
编写SpringMVC配置文件springmvc.xml
xxxxxxxxxx
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描Controller包 -->
<context:component-scan base-package="com.itbaizhan.controller"/>
<!-- 配置视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 开启SpringMVC注解的支持 -->
<mvc:annotation-driven/>
<!-- 放行静态资源 -->
<mvc:default-servlet-handler />
</beans>
编写Spring的总配置文件applicationContext.xml,该文件引入dao和service层的Spring配置文件
xxxxxxxxxx
<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">
<import resource="applicationContext-dao.xml"></import>
<import resource="applicationContext-service.xml"></import>
</beans>
在web.xml中配置Spring监听器,该监听器会监听服务器启动,并自动创建Spring的IOC容器
xxxxxxxxxx
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<!-- 创建spring容器的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
在web.xml中配置SpringMVC的前端控制器和编码过滤器
xxxxxxxxxx
<!--前端控制器-->
<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:springmvc.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>encFilter</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>encFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
编写JSP页面allStudent.jsp
xxxxxxxxxx
<%page contentType="text/html;charset=UTF-8" language="java" %>
<%taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>所有学生</title>
</head>
<body>
<%-- 添加学生表单 --%>
<form action="/student/add" method="post">
姓名:<input name="name">
性别:<input name="sex">
地址:<input name="address">
<input type="submit" value="提交">
</form>
<%-- 展示学生表格 --%>
<table width="500" cellpadding="0" cellspacing="0" border="1" align="center">
<tr>
<th>id</th>
<th>姓名</th>
<th>性别</th>
<th>地址</th>
</tr>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.sex}</td>
<td>${student.address}</td>
</tr>
</c:forEach>
</table>
</body>
</html>