一般情况下,我们会在web.xml下配置好Spring和SpringMVC,并指定好它们的配置文件
是最常用的也是最方便的方法
例如:
web.xml
<!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
今天介绍的是纯java配置,不基于xml,
web.xml中一句代码都不用写。
文件目录:
1.Spring容器的配置文件
package com.mvc.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration //标识是配置文件 @ComponentScan("com.mvc.server") //指定自动扫描的包 public class RootConfig { //在这里可以配置任何的bean,是基于Spring容器的 }
2.SpringMVC容器的配置文件
package com.mvc.config; 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 //表示这个类是SpringMVC的配置文件 @ComponentScan("com.mvc.action")//注意扫描的包 public class WebConfig extends WebMvcConfigurerAdapter{ @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(); } }
3.配置自定义的DispatcherServlet,在tomcat启动的时候,会找到这个类,并自动加载它,所以web.xml中不用再写任何有关Spring配置的代码
package com.mvc.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class MyDispatcherServlet extends AbstractAnnotationConfigDispatcherServletInitializer{ @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[]{RootConfig.class};//加载Spring的配置类 } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[]{WebConfig.class};加载SpringMVC的配置类 } @Override protected String[] getServletMappings() { return new String[]{"/"}; //映射路径 } }
以上,Spring和SpringMVC的配置文件就写好了
4.下面编写控制器,这里的代码就是平时写的类型
package com.mvc.action; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.mvc.server.PersonServer; @Controller public class MyController { @Autowired private PersonServer p; public MyController(){ System.out.println("MyController..."); } @RequestMapping("test") public String test(Map map){ map.put("p", p.getPerson()); System.out.println("test..."); return "home"; } }
5.编写服务层
package com.mvc.server; import org.springframework.stereotype.Service; import com.mvc.entity.Person; @Service public class PersonServer { public PersonServer(){ System.out.println("PersonServer.."); } public Person getPerson(){ return new Person("aaa",34); } }
6.实体类
package com.mvc.entity; public class Person { public Person(){} public Person(String name, Integer age) { this.name = name; this.age = age; } private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
配置好后,启动tomcat,就可以正常访问了。
需要注意的是WebConfig.java、RootConfig.java、MyDispatcherServlet.java这三个配置类,其他的没什么区别