Spring整合web环境

目录

Javaweb三大组件及环境特点

Spring整合web环境的思路及实现

Spring的web开发组件spring-web

MVC框架思想及其设计思路


Javaweb三大组件及环境特点

 

Spring整合web环境的思路及实现

 

package com.xfy.listener;

import com.xfy.config.SpringConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


public class ContextLoaderListenter implements ServletContextListener {
    private  String CONTEXT_CONFIG_LOCATION="ContextConfigLocation";
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        String initParameter = servletContext.getInitParameter(CONTEXT_CONFIG_LOCATION);
//        initParameter=initParameter.substring("classpath".length());
        //1.创建Spring容器
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(initParameter);
        //2.将容器存入ServletContext中
        servletContext.setAttribute("applicationContext",applicationContext);

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}



public class WebAppLicationContext {
    public  static ApplicationContext getWebAppLicationContext(ServletContext servletContext){
        ApplicationContext applicationContext = (ApplicationContext) servletContext.getAttribute("applicationContext");
        return applicationContext;
    }
}




@WebServlet(urlPatterns = "/accountServlet")
public class AccountServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ApplicationContext applicationContext =  WebAppLicationContext.getWebAppLicationContext(request.getServletContext());
        AcountService bean = applicationContext.getBean(AcountService.class);
        bean.transforMoney("tom","lucy",500);
    }

 

Spring的web开发组件spring-web

 Spring其实已经为我们定义好了一个ContextLoaderListener,使用方式跟我们上面自己定义的大体一样

 先导入Spring-web的坐标

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



    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<!--    定义全局参数  -->
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>com.xfy.config.MyxfyApplicationContext</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

 

MVC框架思想及其设计思路

 

 

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架提供了多种方式来整合Web开发。以下是一种常见的方式: 1. 添加依赖:在你的项目中,添加Spring Web的依赖。你可以使用Maven或Gradle来管理依赖。 Maven: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` Gradle: ```groovy implementation 'org.springframework.boot:spring-boot-starter-web' ``` 2. 创建控制器:创建一个控制器类来处理请求和响应。在这个类上使用`@Controller`注解,并在方法上使用`@RequestMapping`注解来映射URL和处理方法。 ```java @Controller public class MyController { @RequestMapping("/hello") public String hello() { return "Hello, World!"; } } ``` 3. 配置Spring MVC:创建一个配置类,使用`@Configuration`注解,并继承`WebMvcConfigurerAdapter`类。在这个类中,可以配置视图解析器、静态资源处理等。 ```java @Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp("/WEB-INF/views/", ".jsp"); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("/static/"); } } ``` 4. 启动应用程序:使用Spring Boot或其他方式启动你的应用程序,Spring会自动扫描并注册控制器。 这只是一个简单的示例,你可以根据你的需求进行更多的配置和定制。希望对你有所帮助!如果还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值