Spring 梳理 - JavaConfig实战

  1.  目录结构
  2. AppInitializer.java
    1. package com.jt;
      
      import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
      
      public class AppInitializer   extends AbstractAnnotationConfigDispatcherServletInitializer{
            @Override
            protected Class<?>[] getRootConfigClasses() {
              return new Class<?>[] { RootConfig.class };
            }
      
            @Override
            protected Class<?>[] getServletConfigClasses() {
              return new Class<?>[] { WebConfig.class };
            }
      
            @Override
            protected String[] getServletMappings() {
              return new String[] { "/" };
            }
      
          }
  3. RootConfig.java
    1. package com.jt;
      
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.context.annotation.ComponentScan.Filter;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.web.servlet.config.annotation.EnableWebMvc;
      
      @Configuration
      public class RootConfig {
      
      }
  4. WebConfig.java
    1. package com.jt;
      
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.web.servlet.ViewResolver;
      import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
      import org.springframework.web.servlet.config.annotation.EnableWebMvc;
      import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
      import org.springframework.web.servlet.view.InternalResourceViewResolver;
      
      @Configuration
      @EnableWebMvc
      @ComponentScan("com.jt")
      public class WebConfig extends WebMvcConfigurerAdapter {
          public WebConfig(){
              System.out.println("WebConfig");
          }
          @Bean
          public ViewResolver viewResolver() {
              InternalResourceViewResolver resolver = new InternalResourceViewResolver();
              resolver.setPrefix("/WEB-INF/views/");
              resolver.setSuffix(".jsp");
              resolver.setExposeContextBeansAsAttributes(true);
              return resolver;
          }
      
          @Override
          public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
              configurer.enable();
          }
      }
  5. HelloControl.java
    1. package com.jt;
      
      import java.sql.Date;
      import java.util.Map;
      
      import javax.validation.Valid;
      
      import org.springframework.stereotype.Component;
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.validation.BindingResult;
      import org.springframework.validation.Errors;
      import org.springframework.validation.ObjectError;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      import org.springframework.web.bind.annotation.RequestParam;
      import org.springframework.web.bind.annotation.RequestMethod.*;
      
      import com.myOrg.DeptUserDomain;
      
      @Controller
      @RequestMapping(value = "/FirstControl")
      public class HelloControl {
          @RequestMapping(value="/registerDeptUser",method=RequestMethod.GET)
          public String register(){
              
              return "registerDeptUser";
          }
      }
  6. registerDeptUser.jsp
    1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form method="post"> id:<input type="text" name="id" /><br /> name:<input type="text" name="name" /><br /> <input type="submit" value="submit" /> </form> </body> </html>
  7. 运行
  8. 参考:https://blog.csdn.net/bufanqi_info/article/details/78922534

猜你喜欢

转载自www.cnblogs.com/jiangtao1218/p/10086896.html