SpringMVC的项目部署已经Config替代web.xml和application-mvc.xml(九)

1.创建一个maven web项目,其中导入springmvc,spring等基本web框架包

2. 项目结构:

 1. 首先创建WebConfig类来代替application-mvc.xml文件配置

/**
     <!-- 1.开启注解扫描 -->
     <context:component-scan base-package="com.fxys.controller"/>
     <!-- 2.支持注解驱动:如json解析  -->
     <mvc:annotation-driven/>
     <!-- 3.配置视图解析器 -->	
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
     </bean> 

 */
@Configuration
@ComponentScan//扫描器
@EnableWebMvc//相当于<mvc:annotation-driven/>,作用后面再详解
public class WebConfig {
	
	@Bean
	public InternalResourceViewResolver internalResourceViewResolver(){
		InternalResourceViewResolver view=new InternalResourceViewResolver();
		view.setSuffix(".jsp");
		view.setPrefix("/WEB-INF/views/");
		return view;
	}

}

 2.创建WebApplication类来代替web.xml文件配置

//实现WebApplicationInitializer接口,以表明他是一个web.xml配置文件作用
public class WebApplication implements WebApplicationInitializer {

	public void onStartup(ServletContext servletContext) throws ServletException {
		// 1.创建一个webapplicationContext
		AnnotationConfigWebApplicationContext ctx = new 
                                        AnnotationConfigWebApplicationContext();
		
		/**
		<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:application-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	    </servlet>
		 */
		// 加载spring/springmvc的配置文件类
		ctx.register(WebConfig.class);
		//绑定servletContext到配置文件中
        ctx.setServletContext(servletContext);
        
		// 2.注册一个DispatcherServlet
        /**
        <servlet-mapping>
		   <servlet-name>springMVC</servlet-name>
		   <url-pattern>/</url-pattern>
	    </servlet-mapping>
         */
        Dynamic dispatcherServlet=
s           ervletContext.addServlet("dispatcherServlet", new DispatcherServlet(ctx));
	    dispatcherServlet.addMapping("/");
	    dispatcherServlet.setLoadOnStartup(1);
	    
	    //注册filter,注意不同的Dynamic要有不同的包
	    /**
       <!--定义编码 -->
	    <filter>
		  <filter-name>Spring</filter-name>
	          <filter-class>
                org.springframework.web.filter.CharacterEncodingFilter
              </filter-class>
		      <init-param>
			     <param-name>encoding</param-name>
			     <param-value>utf-8</param-value>
		      </init-param>
	    </filter>
	    <filter-mapping>
		   <filter-name>Spring</filter-name>
		   <url-pattern>/*</url-pattern>
	    </filter-mapping>
	     */ 
	    javax.servlet.FilterRegistration.Dynamic filter= 
                 servletContext.addFilter("encodingFilter",
                     new CharacterEncodingFilter("utf-8",true));
	    filter.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST),
                                    true, "dispatcherServlet");
	}

}

 3.创建HelloController类测试一下

@Controller
public class HelloController {
	
	@RequestMapping("/input")
	@ResponseBody
	public String input(String value){
		System.out.println(value);
		return "success";
	}
}

 4.创建一个form.jsp页面用于检测是否编码设置成功以及springmvc的配置是否成功

<body>
	<form action="/input" method="post">
	   <input type="text" name="value"/>
	   <input type="submit" value="submit"/>
	</form>
</body>

5.注意:maven的tomcat插件配置目录为/根路径,忽略项目名

			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<!-- tomcat启动的端口 -->
					<port>80</port>
					<!-- 应用的上下文路径 -->
					<path>/</path>
					<!--解决中文乱码 -->
					<uriEncoding>UTF-8</uriEncoding>
				</configuration>
				<!-- 在打包的时候运行这个容器 -->
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

 6.访问页面进行测试

 输出:

部署成功!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值