1、Spring框架中使用Servlet、jsp开发需要包:
2、Spring配置文件的配置(Spring-web包):
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!--配置上下文文件-->
<context-param>
<!--此处的名称不能随意修改-->
<param-name>contextConfigLocation</param-name>
<!--配置Spring的配置文件-->
<param-value>classpath:ApplicationSpringMyBatis.xml</param-value>
</context-param>
<!--封装一个监听,当使用tomcat启动项目时帮忙加载Spring的配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
此处创建监听需要用到Spring-web包中的监听类,该类加载时会查找名称为contextConfigLocation的参数,因此需要在配置上下文是在<context-param>中配置此名的参数(<param-name>),则加载监听类时会默认加载<param-value>中配置的所有.xml文件(配置多个xml文件时,以逗号相隔);
如果未配置此名称的上下文参数,则会在加载监听类时默认加载ApplicationContext.xml文件:
3、servlet中配置文件的使用(以addUser为例):
AddUserServlet.java
package Servlet;
import Demo01.Model.User;
import Demo01.dao.Interface.UserInfo;
import Demo01.dao.UserImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
@WebServlet("/insertUser")
public class AddUserServlet extends HttpServlet {
private UserInfo user;
@Override
public void init() throws ServletException {
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
user = ac.getBean("userImpl", UserImpl.class);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置请求参数的编码方式
req.setCharacterEncoding("UTF-8");
//获取请求参数
String username = req.getParameter("username");
char sex = req.getParameter("sex").charAt(0);
String address = req.getParameter("address");
User u = new User();
u.setUsername(username);
u.setSex(sex);
u.setAddress(address);
u.setBirthday(new Date());
int index = user.insertUser(u);
//插入之后调用请求的重定向方法返回用户列表页
/**
* 此处使用重定向方法,而不是使用请求转发,是因为使用请求转发可能会导致重复提交的问题
*/
if(index>0)
resp.sendRedirect("/findAllUser");
else
resp.sendRedirect("addUser.jsp");
}
}
注:1、当时会用web3.0和tomcat7以上版本进行Servlet开发时,可以在Servlet类前使用注解开发(***例如:@WebServlet("/insertUser")***),不用在web.xml中配置;
2、创建Servlet类时需要继承HttpServlet父类,重写init()方法,这是初始化方法,在访问该servlet类时先走init(),因此可以在此获取Spring配置文件信息
4、用户新增JSP(JSP需要用到包 jstl和taglibs) :
addUser.jsp:
<%--
Created by IntelliJ IDEA.
User: xingfuhu
Date: 2020/4/13
Time: 0:04
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>新增用户信息</title>
</head>
<body>
<form action="insertUser" method="post">
<table border="1">
<tr>
<td colspan="2" align="center" style="font-size: 30px;">新增用户信息</td>
</tr>
<tr>
<td align="center" style="font-size: 20px;">用户名称:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td align="center" style="font-size: 20px;">性别:</td>
<td><input type="text" name="sex"></td>
</tr>
<tr>
<td align="center" style="font-size: 20px;">住址:</td>
<td><input type="text" name="address"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" align="center" value="提交">
<input type="reset" align="center" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
5、查询所有用户的Servlet文件:
package Servlet;
import Demo01.Model.User;
import Demo01.dao.Interface.UserInfo;
import Demo01.dao.UserImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.List;
/**
* 重写继承类的方法的快捷键
* 1 用alt+insert
* 2 ctrl + o
*
*
* @WebServlet("/findAllUser") 注解意思是拦截以这方式的请求
*/
@WebServlet("/findAllUser")
public class ServletService extends HttpServlet {
private UserInfo user = null;
@Override
public void init() throws ServletException {
ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
user = ac.getBean("userImpl", UserImpl.class);
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
//super.service(req, res); //此处存在会报错
List<User> list = user.queryAllUser();
//使用req的作用于,设置查询结果
req.setAttribute("userList",list);
//使用req作用于,需要使用req的请求转发
req.getRequestDispatcher("index.jsp").forward(req,res);
}
}
6、查询用户的JSP;
index.jsp:
<%@page contentType="text/html; utf-8" pageEncoding="UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<style type="text/css">
a{
color:black;
text-decoration: none; //取消a标签的下划线
}
a:hover{
color:red;
}
</style>
<body>
<h2><a href="./addUser.jsp">用户新增</a></h2>
<table border="1">
<tr>
<th>用户Id</th>
<th>姓名</th>
<th>性别</th>
<th>生日</th>
<th>住址</th>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.sex}</td>
<td>${user.birthday}</td>
<td>${user.address}</td>
</tr>
</c:forEach>
</table>
</body>
</html>