• 吴裕雄天生自然SPRING基于Java配置的Spring MVC应用


    使用web.xml和springmvc-servlet.xml配置文件进行Web配置和Spring MVC配置。但Spring Boot推荐使用Java配置的方式进行项目配置
    创建Spring MVC的Java配置
    
    在该配置类中使用@Configuration注解声明该类为Java配置类;使用@EnableWebMvc注解开启默认配置,如ViewResolver;使用@ComponentScan注解扫描注解的类;使用@Bean注解配置视图解析器;该类需要实现WebMvcConfigurer接口来配置Spring MVC。
    创建Web的Java配置
    
    创建Web的Java类WebConfig。该类需要实现WebApplicationInitializer接口替代web.xml文件的配置。实现该接口将会自动启动Servlet容器。在WebConfig类中需要使用AnnotationConfigWebApplicationContext注册Spring MVC的Java配置类SpringMVCConfig,并和当前ServletContext关联。最后,在该类中需要注册Spring MVC的DispatcherServlet。
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        没注册的用户,请<a href="index/register">注册</a><br>
        已注册的用户,去<a href="index/login">登录</a></body>
    </html>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        登录页面!
    </body>
    </html>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        注册页面!
    </body>
    </html>
    package config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    
    @Configuration
    @EnableWebMvc
    @ComponentScan("controller")
    public class SpringMVCConfig implements WebMvcConfigurer {
        /**
         * 配置视图解析器
         */
        @Bean
        public InternalResourceViewResolver getViewResolver() {
            InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
            viewResolver.setPrefix("/WEB-INF/jsp/");
            viewResolver.setSuffix(".jsp");
            return viewResolver;
        }
    
        /**
         * 配置静态资源
         */
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/html/**").addResourceLocations("/html/");
        }
    }
    package config;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRegistration.Dynamic;
    
    import org.springframework.web.WebApplicationInitializer;
    import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
    import org.springframework.web.servlet.DispatcherServlet;
    
    public class WebConfig implements WebApplicationInitializer {
        @Override
        public void onStartup(ServletContext arg0) throws ServletException {
            AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
            ctx.register(SpringMVCConfig.class);// 注册Spring MVC的Java配置类SpringMVCConfig
            ctx.setServletContext(arg0);// 和当前ServletContext关联
            /**
             * 注册Spring MVC的DispatcherServlet
             */
            Dynamic servlet = arg0.addServlet("dispatcher", new DispatcherServlet(ctx));
            servlet.addMapping("/");
            servlet.setLoadOnStartup(1);
        }
    }
    package controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * “@Controller”表示IndexController的实例是一个控制器
     * 
     * @Controller相当于@Controller("indexController") 或@Controller(value =
     *                                              "indexController")
     */
    @Controller
    @RequestMapping("/index")
    public class IndexController {
        @RequestMapping("/login")
        public String login() {
            /**
             * login代表逻辑视图名称,需要根据Spring MVC配置 文件中internalResourceViewResolver的前缀和后缀找到对应的物理视图
             */
            return "login";
        }
    
        @RequestMapping("/register")
        public String register() {
            return "register";
        }
    }
  • 相关阅读:
    uniGUI试用笔记(九)uniGUI执行程序部署有3种形式1
    CxGrid导出Excel时清除颜色的设置
    delphi 安卓程序如何读取外部配置文件
    cxgrid动态显示行号
    为Delphi程序增加UAC功能(管理员身份运行exe)
    delphi debug release区别是什么?
    启动程序的同时传参给接收程序(XE8+WIN764)
    iOS Development和iOS Distribution有什么区别
    4张图看懂delphi 10生成ipa和在iPhone虚拟器上调试(教程)
    使用Xcode 7 beta免费真机调试iOS应用程序
  • 原文地址:https://www.cnblogs.com/tszr/p/15312020.html
Copyright © 2020-2023  润新知