SpringMVC入门小程序 -- Myeclipse 9.1下

注:本文项目文件在https://github.com/SpaceyLi/SpringMVC_test

一、下载

    首先需要到官网http://repo.spring.io/release/org/springframework/spring/下载spring包(本文使用3.2.10),此外还需要下载aopalliance.jar包。

    环境说明:

        IDE:MyEclipse9.1

        JDK:1.6.0_45

        Tomcat:7.0.42

二、创建工程

    创建工程SpringMVC_test(Web Project),本文的项目路径为E:\MyEclipse\SpringMVC_test,在J2EE Specification Level选项中选择Java EE 6.0。

三、配置

    1.项目建好后把事先下载好的spring包解压后打开,将spring-framework-3.2.10.RELEASE\libs中的所有非javadoc和sources包copy到项目的SpringMVC_test/WebRoot/WEB-INF/lib文件夹下,然后刷新项目。就可以在MyEclipse的Package Explorer窗口中看到这些库文件。

    2.修改WebRoot/WEB-INF/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/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	
	<display-name>SpringMVC_test</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	
	<!-- spring -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:config/applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- springMVC -->
	<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*:config/springMVC.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>
	
	<!-- character encoding filter -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

    3.在src下添加文件夹config,分别创建springMVC.xml和applicationContext.xml

        springMVC.xml:

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd            
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-3.1.xsd         
		   http://www.springframework.org/schema/mvc 
		   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<!-- scan component -->
    <context:component-scan base-package="com.spacey.demo">
 		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
  		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> 
    </context:component-scan>

	<!-- view resolver -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	
	<!-- annotation method1 -->
	<mvc:annotation-driven />
	<!-- annotation method2 -->
<!--	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" lazy-init="false"></bean>
-->
	
	<!-- file upload -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8" />
		<property name="maxUploadSize" value="10485760000" />
		<property name="maxInMemorySize" value="40960" />
	</bean>

	<!-- static file access1 -->
 	<mvc:default-servlet-handler/>
	<!-- static file access2 -->
<!--<mvc:resources location="/img/" mapping="/img/**"/>
	<mvc:resources location="/js/" mapping="/js/**"/>
 -->

</beans>

        applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:task="http://www.springframework.org/schema/task" 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:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
	xmlns:flex="http://www.springframework.org/schema/flex" xmlns:amq="http://activemq.apache.org/schema/core"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/task 
           http://www.springframework.org/schema/task/spring-task-3.1.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
           http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring     
  		   http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd
  		   http://www.springframework.org/schema/flex
		   http://www.springframework.org/schema/flex/spring-flex-1.5.xsd
		   http://www.springframework.org/schema/mvc 
		   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	
	
	<!-- auto scan component -->
	<!-- 对于用annotation方式的事务注解和bean配置,spring的配置文件 与springMVC的配置文件对包的重复扫描装配会照成事务失效 
			因为spring的context是父子容器,所以会产生冲突,由ServletContextListener产生的是父容器,springMVC产生的是子容器, 
			子容器Controller进行扫描装配时装配了@Service注解的实例,而该实例理应由父容器进行初始化以保证事务的增强处理, 所以此时得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力。) 
			在主容器中(applicationContext.xml),将Controller的注解排除掉 -->
 	<context:component-scan base-package="com.spacey.demo">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	
</beans>

    4.在package com.spacey.demo下创建类HelloWorldController.java:

package com.spacey.demo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("hello/")
public class HelloWorldController{
	
	@RequestMapping("toHelloJsp")
	public String toHelloJsp(ModelMap modelMap) throws Exception{
		System.out.println("----------------------here---------------------------");
		modelMap.put("message", "==test==");
		return "hello";
	}
}

    5.在WebRoot/WEB-INF/下创建文件夹jsp,并在其中添加hello.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>hello world</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  
  <body>
    hello world!${message }<br>
  </body>
</html>

    6.运行,部署即可访问http://localhost:8080/hello/toHelloJsp。

你可能感兴趣的:(springMVC,MyEclipse,入门,实例)