公司转Java开发,作的第一个项目是SpringMVC框架,由于底层是同事封装,等完成整个项目,对SpringMVC框架的搭建还不是很了解,因此抽时间不忙的时候本身搭建了一个SpringMVC框架。css
本次搭建SpringMVC想实现的效果很简单,就是可以在浏览其中直接访问Controller层,实现HelloWord的展现。前端
一、框架的大致结构:java
2.pom.xml主要是jar包引入web
pom.xml:spring
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.py</groupId> <artifactId>SpringMvcDemo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> </dependencies> </project>
其中,build节点是很重要的节点,主要是程序编译组件的加载,若是没有该节点,项目启动不起来。express
引入的必须jar包如上述代码所示。apache
三、web.xml配置api
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 配置前端控制器 --> <!--<servlet>--> <!--<servlet-name>web-dispatcher</servlet-name>--> <!--<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>--> <!--<init-param>--> <!--<param-name>contextConfigLocation</param-name>--> <!--<param-value>classpath:spring-mvc.xml</param-value>--> <!--</init-param>--> <!--<load-on-startup>1</load-on-startup>--> <!--</servlet>--> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加载前端控制器配置文件 上下文配置位置--> <init-param> <!-- 备注: contextConfigLocation:指定 SpringMVC 配置的加载位置,若是不指定则默认加载 WEB-INF/[DispatcherServlet 的 Servlet 名字]-servlet.xml --> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <!-- 表示随WEB服务器启动 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 备注:能够拦截三种请求 第一种:拦截固定后缀的url,好比设置为 *.do、*.action, 例如:/user/add.action 此方法最简单,不会致使静态资源(jpg,js,css)被拦截 第二种:拦截全部,设置为/,例如:/user/add /user/add.action此方法能够实现REST风格的url, 不少互联网类型的应用使用这种方式.可是此方法会致使静态文件(jpg,js,css)被拦截后不能正常显示.须要特殊处理 第三种:拦截全部,设置为/*,此设置方法错误,由于请求到Action,当action转到jsp时再次被拦截,提示不能根据jsp路径mapping成功 --> <!-- 默认匹配全部的请求 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
四、spring-mvc.xml配置spring-mvc
<?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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter, --> <!-- 可用在xml配置文件中使用<mvc:annotation-driven>替代注解处理器和适配器的配置。 --> <mvc:annotation-driven/> <!-- 组件扫描器:能够扫描 @Controller、@Service、@Repository 等等 --> <context:component-scan base-package="org.py.heaton.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
五、web.xml服务器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 配置前端控制器 --> <!--<servlet>--> <!--<servlet-name>web-dispatcher</servlet-name>--> <!--<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>--> <!--<init-param>--> <!--<param-name>contextConfigLocation</param-name>--> <!--<param-value>classpath:spring-mvc.xml</param-value>--> <!--</init-param>--> <!--<load-on-startup>1</load-on-startup>--> <!--</servlet>--> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加载前端控制器配置文件 上下文配置位置--> <init-param> <!-- 备注: contextConfigLocation:指定 SpringMVC 配置的加载位置,若是不指定则默认加载 WEB-INF/[DispatcherServlet 的 Servlet 名字]-servlet.xml --> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <!-- 表示随WEB服务器启动 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 备注:能够拦截三种请求 第一种:拦截固定后缀的url,好比设置为 *.do、*.action, 例如:/user/add.action 此方法最简单,不会致使静态资源(jpg,js,css)被拦截 第二种:拦截全部,设置为/,例如:/user/add /user/add.action此方法能够实现REST风格的url, 不少互联网类型的应用使用这种方式.可是此方法会致使静态文件(jpg,js,css)被拦截后不能正常显示.须要特殊处理 第三种:拦截全部,设置为/*,此设置方法错误,由于请求到Action,当action转到jsp时再次被拦截,提示不能根据jsp路径mapping成功 --> <!-- 默认匹配全部的请求 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
六、Controller层:
package org.py.heaton.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * Created by py on 2017/1/5. */ @Controller @RequestMapping(value = "/Index/") public class IndexController { @ResponseBody @RequestMapping(value = "HelloWord") public String HelloWord(){ return "Hello Word!!!!"; } }
七、访问Controller,有图可见,能够正常访问