SpringMVC入门程序

来源:互联网 发布:天书世界圣物进阶数据 编辑:程序博客网 时间:2024/06/04 18:48

搭建一个SpringMVC入门程序

前言


一枚大四的菜鸡,写这个主要是SpringMVC框架的入门程序,搭建一个最最最简单的程序(若是刚接触Spring,这篇可以帮助你也搭建一个入门程序)。只是简单的使用,若是以后研究的透彻一些会更新一些深层次的知识。再说一些心(wu)灵(nai)鸡(zhi)汤(ju)吧。大学四年的学习生活自己基本上就是玩了,面对毕业招聘季,苍白的简历不知如何去装饰,只能靠自己的“专业技能知识”去博得工作的机会吧。然而当自己完全没有网络的工具会显得无助。就是连最基本的框架环境都搭建不出来。以前学习过的东西长时间不用全都忘掉了。所以废话不说了,自己多写一些东西,一方面是让自己更加熟练吃饭的家伙,另一方面就是以后忘记的时候可以看自己的东西更快熟悉。

创建一个web项目

这里写图片描述

导入需要使用的jar包

这里写图片描述

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>  <display-name>SSM</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>  </welcome-file-list>  <!-- 配置SpringMVC的前端控制器,也就是所有的请求都交有Spring处理 -->  <servlet>    <servlet-name>spring</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:beans.xml</param-value>    </init-param>  </servlet>  <!-- 将所有的请求都进行过滤,也就是交由Spring处理 -->  <servlet-mapping>    <servlet-name>spring</servlet-name>    <url-pattern>*.tt</url-pattern>  </servlet-mapping></web-app>

创建spring的配置文件

使用非注解方式配置
<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    <bean name="/mvc.tt" class="handler.NoHandler"></bean>    <!-- 配置处理器映射器 -->    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>    <!-- 配置处理器适配器-->    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>    <!-- 配置视图解析器 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/></beans>
public class NoHandler implements Controller{    public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {        ModelAndView mv = new ModelAndView();        mv.setViewName("index.html");        return mv;    }}
使用注解方式配置
<!-- 扫描handler包 -->    <context:component-scan base-package="handler"></context:component-scan>    <!-- 配置注解映射器 -->    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>    <!-- 配置注解适配器 -->    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>    <!-- 使用如下方式,可以代替上面的映射器和适配器 -->    <mvc:annotation-driven></mvc:annotation-driven>
@Controllerpublic class AnnoHandler {    @RequestMapping("anno.tt")    public ModelAndView annoMVC(HttpServletRequest request, HttpServletResponse response) {        ModelAndView mv = new ModelAndView();        mv.setViewName("index.html");        return mv;    }}

测试最后的结果

非注解测试
这里写图片描述
注解测试结果
这里写图片描述

原创粉丝点击