【Spring MVC】完成Spring 3 MVC的第一个实例

来源:互联网 发布:网络研修困惑 编辑:程序博客网 时间:2024/05/16 19:33

刚从一个项目中出来,发现好多东西都不懂,带着诸多疑问,开启了J2EE探索路。

第一篇,从Spring MVC开始,本文只写操作步骤,不做任何解释,因为我还不懂,只求达到成功体验Spring MVC冰山一角的目的。

开发环境说明

JDK:jdk-6u32-windows-i586Eclipse:eclipse-jee-juno-SR1-win32Tomcat:apache-tomcat-7.0.30Spring:spring-framework-3.1.2.RELEASE、spring-framework-3.0.2.RELEASE-dependencies

自己去准备吧。

新建Web工程

新建Web工程,工程名SpringMVC,修改工程字符集为UTF-8,其他没什么好说的,这里主要说下jar包。

先导入spring的相关jar包,在spring-framework-3.1.2.RELEASE/dist目录下,导入除了下面三个文件的剩余全部jar包:

org.springframework.spring-library-3.1.2.RELEASE.libdorg.springframework.web.struts-3.1.2.RELEASE.jarorg.springframework.web.portlet-3.1.2.RELEASE.jar

再导入Spring的依赖包,在spring-framework-3.0.2.RELEASE-dependencies里找,具体jar包如下:

com.springsource.org.apache.commons.logging-1.1.1.jarcom.springsource.org.aopalliance-1.0.0.jar

导入完成后如下图:


web.xml文件

在web.xml中加入spring mvc的配置信息,完整的web.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"><display-name>SpringMVC</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- Spring MVC --><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*:resources/spring/spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>

spring-mvc.xml文件

先给出本工程的目录结构:


在resources.spring包下新建spring-mvc.xml文件,完整内容如下:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 --><mvc:annotation-driven /><!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean --><context:component-scan base-package="com.lewis" /><!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/pages/" /><property name="suffix" value=".jsp" /></bean></beans>

编写Controller

com.lewis.test.TestController。

package com.lewis.test;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controller@RequestMapping(value="/test")public class TestController {public TestController() {}@RequestMapping(value = "/welcome", method = RequestMethod.GET)public String someMethod() {return "/welcome";}}

编写页面

在WebContent下新建文件夹pages,在pages下新建welcome.jsp。

<%@ page language="java" contentType="text/html; charset=UTF-8"%><html><head><title>welcome</title></head><body>welcome</body></html>

最后一步

通过http://localhost:8080/SpringMVC/test/welcome访问,结果如下图:

原创粉丝点击