Spring-MVC框架

来源:互联网 发布:淘宝买dota2饰品安全吗 编辑:程序博客网 时间:2024/06/09 22:27

Spring-MVC项目框架的搭建
1.需要的文件准备
1)spring-framework-4.3.0.RELEASE-dist.zip,新官网下载教程:
http://blog.csdn.net/evano_o/article/details/53423249。
2)commons-logging-1.2.jar文件,下载地址为:
http://commons.apache.org/proper/commons-logging/download_logging.cgi。

2.项目搭建(使用的IDE为Eclipse)
1)配置Tocat我使用的为免安装版,解压缩即可,在eclipse中点击Window->Preferences->Server->Runtime Environment。
Add->Apache Tomcat v8.0->Browse选择tomcat的解压目录。Finish就可以。
2)创建工程,创建JAVAEE工程的步骤为:New->Other->Web->Dynamic Web Project。在创建的最后一步应勾选“Generate web.xml deloyment descriptor“。创建项目后需要依赖Tomcat的一些运行jar包,依赖步骤为右键项目->Java Build Path->Configure Build PathLibraries->Add Library->Server Runtime->Next->Apache Tomcat,OK。
3)将spring-framework-4.3.0.RELEASE-dist.zip解压缩,将解压后的文件夹中libs文件夹下的所有jar文件复制到项目的/WebLearn/WebContent/WEB-INF/lib目录下,还需要Spring的依赖jar包:commons-logging-1.2.jar一同复制到该目录下。然后添加依赖,步骤:右键项目->Java Build Path->Configure Build PathLibraries->Libraries->Add JARS,选中刚刚复制jar的文件夹中所有jar文件,点击OK。可在项目的结构中看到依赖的库文件:

这里写图片描述

项目依赖文件
3.写配置文件(web.xml)
先新建资源文件夹:resources,在新建spring包,并新建xml文件:ApplicationContext-mvc.xml。

<servlet>        <servlet-name>springmvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <!-- Spring配置文件路径,classpath:是项目的资源文件路径 -->            <param-value>classpath:spring/ApplicationContext-mvc.xml</param-value>        </init-param>        <!-- 启动时立即加载Servlet -->        <load-on-startup>1</load-on-startup>    </servlet>    <!-- Servelt映射说明 -->    <servlet-mapping>        <servlet-name>springmvc</servlet-name>        <!-- 监听所有请求 -->        <url-pattern>/</url-pattern>    </servlet-mapping>

4.配置SpringMVC文件(ApplicationContext-mvc.xml)
在项目的src目录下新建wzq.web.test.controller包,并新建HelloController.java。

<?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:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd">    <!-- 启用注解 -->    <context:annotation-config />    <!-- Controller扫描的包 -->    <context:component-scan base-package="wzq.web.test.controller" />    <!-- 处理映射器,处理器适配器。 Spring 4.0之后就不需要显示的声明。    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>    -->    <!-- 配置SpringMVC的视图解析器 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    </bean></beans>

5.编写JAVA文件(HelloController.java)
在/WEB-INF目录下新建jsp文件夹,并新建welcome.jsp。

@Controllerpublic class HelloController {    @RequestMapping(value="/hello")    public ModelAndView handleHelloRequest(){        System.out.println("被调用");        ModelAndView mv = new ModelAndView();        mv.addObject("message", "Hello World"); //object对应jsp中的requestScope.后的键名称。        mv.setViewName("/WEB-INF/jsp/welcome.jsp");        return mv;    }}

引入了Spring的注解,该方法处理的为”/项目名/hello”请求地址。
注意其中的ModelAndView类为 org.springframework.web.servlet.ModelAndView类。

6.编写jsp(welcome.jsp)

jsp代码为:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Welcome</title></head><body>${requestScope.message}</body></html>

7.测试

运行Server。右键项目->Run As->Run on Server,选择tomcat8。
在浏览器中输入地址http://localhost:8080/项目名/hello,可看到界面上的输入文字:
这里写图片描述