Spring学习-03 Spring整合Servlet

Spring学习-03 Spring整合Servlet

文件目录结构

pom.xml
src/main
      │ java/com/min/spring/controller/HelloController.java
      │ resources/spring-context.xml
      │ webapp
        │   hello.jsp
        │   index.html
        └─WEB-INF
            │ web.xml

03.01 追加依赖(pom.xml)

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
</dependency>

spring-web:在web.xml中配置的listener:ContextLoaderListener来自这个包

javax.servlet-api:这个包在写Controller的时候需要用到

03.02 添加一个Controller(HelloController.java)

  • 配置一个Controller /hello,跳转到hello.jsp,并且传递一个参数message
@Controller
public class HelloController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
        					throws ServletException, IOException {
        req.getSession().setAttribute("message", "你好,name");
        resp.sendRedirect(req.getContextPath() + "/hello.jsp");
    }
}

03.03 添加一个hello.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello</title>
</head>
<body>
   hello.jsp,${message}
</body>
</html>

03.04 配置spring的配置文件(spring-context.xml)

  • 启动注解:context:annotation-config
  • 指定扫描的包:context:component-scan
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.min.spring"></context:component-scan>

</beans>

03.05 添加web.xml

  • 配置listener:org.springframework.web.context.ContextLoaderListener
  • 配置spring的上下文的配置文件:contextConfigLocation
  • 配置Controller地址:/hello
<?xml version="1.0" encoding="UTF-8"?>
<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">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-context*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    
	<!--配置controller-->
    <servlet>
        <servlet-name>helloServlet</servlet-name>
        <servlet-class>com.min.spring.controller.HelloController</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值