胖哥学SpringMVC:Hello World 注解版

Hello World 注解版

我的建议从新练习一遍搭建的过程,若是感受麻烦你能够直接复制上一个工程,可是须要修改pom.xml中的一点信息php

<groupId>com.hanpang.springmvc</groupId>
<artifactId>springmvc-helloword-annotation</artifactId>
<version>0.0.1-SNAPSHOT</version>
复制代码

修改artifactId即 , Controller仍是用上一次的便可java

配置核心的类文件

package com.hanpang.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.hanpang.**.web")
public class SpringMvcConfiguration {

}

复制代码

等价于web

<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
    <!-- 1.启动SpringMVC注解 -->
    <mvc:annotation-driven/>
    <!-- 2.扫描注解 -->
    <context:component-scan base-package="com.hanpang.**.web"/>
</beans>

复制代码

第一种初始化加载核心类文件

package com.hanpang.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class HelloWorldInitializer implements WebApplicationInitializer {

	@Override
	public void onStartup( ServletContext application ) throws ServletException {
		System.out.println("开始加载容器");
		AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
		ctx.register(SpringMvcConfiguration.class);//核心类
		ctx.setServletContext(application);

		ServletRegistration.Dynamic servlet = application.addServlet("dispatcher", new DispatcherServlet(ctx));

		servlet.setLoadOnStartup(2);
		servlet.addMapping("/");

	}

}
复制代码

第二种初始化加载核心类文件

这个推荐使用spring

package com.hanpang.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class[] {SpringMvcConfiguration.class};//核心类
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		return null;
	}

	@Override
	protected String[] getServletMappings() {
		return new String[] {"/"};
	}

}
复制代码

Tomcat7插件运行程序

tomcat7:run -Dmaven.tomcat.uriEncoding=UTF-8 -Dmaven.tomcat.path=/ -Dmaven.tomcat.port=8080
复制代码

只要在控制台能显示Hello Worldspring-mvc

相关文章
相关标签/搜索