动态web项目(Dynamic Web Project)的springmvc第一个项目(一)

最近学习了Spring MVC ,还是那句老话,难者不会,会者不难。

一、MVC概述

1、什么是Spring MVC?

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。

使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如Struts1,Struts2等。

2、环境配置

Eclipse:Mars Release (4.5.0)

Apache-Tomcat:8.0.38

JDK:1.8

Spring-framework: 4.2.6.RELEASE

二、程序实现

1、按照常规New 一个Dynamic Web Project项目并且导入Spring MVC相关包

Spring MVC相关包:

2、添加配置文件:在WEB-INF文件夹下面添加web.xml和springmvc.xml文件

在WEB-INF文件夹下面web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SpringMVC_Test</display-name>
  
  <!-- 监听spring上下文容器 -->
  <listener>
  	<listener-class>
  			org.springframework.web.context.ContextLoaderListener 
  	</listener-class>	
  </listener>
  
  <!-- 加载spring的xml配置文件到spring的上下文容器中 -->
  <context-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>/WEB-INF/springmvc.xml</param-value>
  </context-param>
  
  <!-- 配置springmvc DispatcherServlet  -->
  <servlet>
  	<servlet-name>spring</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- DispatcherServlet实际上继承了Servlet,本质上他就是一个servlet。  -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>/WEB-INF/springmvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 配置DispatcherServlet需要拦截的url 把所有请求交给他处理-->
  <servlet-mapping>
  	<servlet-name>spring</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  
  
<!-- 静态资源交给容器处理  把WEB-INF下的静态资源交给Spring处理-->
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>/assets/*</url-pattern>
		<url-pattern>*.ico</url-pattern>
		<url-pattern>*.html</url-pattern>
		<url-pattern>*.txt</url-pattern>
		<url-pattern>*.js</url-pattern>
		<url-pattern>*.css</url-pattern>
		<url-pattern>/fonts/*</url-pattern>
		<url-pattern>/images/*</url-pattern>
		<url-pattern>/upload/*</url-pattern>
		<url-pattern>/create/*</url-pattern>
		<url-pattern>/js/*</url-pattern>
		<url-pattern>/script/*</url-pattern>
		<url-pattern>/style/*</url-pattern>
		<url-pattern>/area/*</url-pattern>
		<url-pattern>/mobile/*</url-pattern>
		<url-pattern>/video/*</url-pattern>
		<url-pattern>/panorama/*</url-pattern>
	</servlet-mapping>

<!-- 欢迎页 -->
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

在WEB-INF文件夹下面配置springmvc的配置文件为:

<?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"
	xmlns:websocket="http://www.springframework.org/schema/websocket"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
						http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
						http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd
						http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
						http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
						http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
						http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
		<!-- spring配置 -->
		<!-- 通过component-scan让spring扫描package下的所有类,让spring的注解生效-->
		<context:component-scan base-package="config"></context:component-scan>
		<context:component-scan base-package="joan.controller"></context:component-scan>
		
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/views/"></property><!-- 作为Controller中return "main";这段字符串的前置字符串 -->
			<property name="suffix" value=".html"></property><!-- 作为Controller中return "main";这段字符串的后置字符串 -->
			<!-- 比如Controller中return "main";其实得到的是return "/WEB-INF/views/main.html"  -->
		</bean>

	
</beans>

4、在WebContent/WEB-INF文件夹下面添加index.html文件:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SpringMVC</title>
</head>
<body>
Hellow Word ,Spring MVC Successful!0.0
</body>
</html>

5、启动服务器,由于本文仅探讨Spring MVC ,所以对配置服务器不做详细阐述

三、运行效果

1、最终项目结构:

2、运行时的截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值