首页 > 其他分享 >Spring-13-监听器

Spring-13-监听器

时间:2022-10-31 15:01:31浏览次数:40  
标签:13 String Spring request ctx 监听器 import servlet javax


Spring-13-监听器_spring


格外加入三个依赖

<!-- servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- jsp依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>

<!--为了使用监听器对象,加入依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
</dependencies>

更改web版本,

Spring-13-监听器_spring_02


Spring-13-监听器_spring_03


不用注解

Spring-13-监听器_spring_04


实例化

Spring-13-监听器_xml_05

Servlet的内容

package com.bjpowernode.controller;

import com.bjpowernode.domain.Student;
import com.bjpowernode.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String strId = request.getParameter("id");
String strName = request.getParameter("name");
String strEmail = request.getParameter("email");
String strAge = request.getParameter("age");

//创建spring的容器对象
//String config= "spring.xml";
//ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
WebApplicationContext ctx = null;
//获取ServletContext中的容器对象,创建好的容器对象,拿来就用
/*String key = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
Object attr = getServletContext().getAttribute(key);
if( attr != null){
ctx = (WebApplicationContext)attr;
}*/

//使用框架中的方法,获取容器对象
ServletContext sc = getServletContext();
ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
System.out.println("容器对象的信息========"+ctx);

//获取service
StudentService service = (StudentService) ctx.getBean("studentService");
Student student = new Student();
student.setId(Integer.parseInt(strId));
student.setName(strName);
student.setEmail(strEmail);
student.setAge(Integer.valueOf(strAge));
service.addStudent(student);

//给一个页面
request.getRequestDispatcher("/result.jsp").forward(request,response);

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}
}

使用监听器

Spring-13-监听器_xml_06


web.xml注册监听器

Spring-13-监听器_xml_07


Spring-13-监听器_spring_08


Spring-13-监听器_xml_09


Spring-13-监听器_spring_10


Spring-13-监听器_java_11


Spring-13-监听器_java_12


标签:13,String,Spring,request,ctx,监听器,import,servlet,javax
From: https://blog.51cto.com/u_15854304/5809774

相关文章