SpringMVC 3.1 体验

使用过ASP.net MVC 3.1发现SprintMVC3.1与其基本类似使用非常简单,与以往的Struts2不同。

下面说说SpringMVC3.1的使用

环境说明:

Eclipse Indigo Service Release 2

EclipseJava EE IDE for Web Developers

java jdk 1.6u31

tomcat 6.0.35

MySQL 5.0.95

mysql-workbench-gpl-5.2.38-win32.msi,这个GUI工具比旧版本好很多。例如:在旧版输入中文非常困难再次版本不存在此问题。

设置JAVA_HOME TOMCAT_HOME环境变量

SpringMVCJarFiles:

commons-logging.jar

jstl-api-1.2.jar

org.springframework.asm-3.1.1.RELEASE.jar

org.springframework.beans-3.1.1.RELEASE.jar

org.springframework.context-3.1.1.RELEASE.jar

org.springframework.core-3.1.1.RELEASE.jar

org.springframework.expression-3.1.1.RELEASE.jar

org.springframework.web-3.1.1.RELEASE.jar

org.springframework.web.servlet-3.1.1.RELEASE.jar


启动Eclipse

1.创建Dynamic Web Project

2.将上述jar files拷贝如到WebContent > WEB-INF > lib文件夹

3.创建类HelloWorldController

package net.viralpatel.spring3.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {

@RequestMapping("/hello") //控制器名,在jsp中使用的
public ModelAndView helloWorld() {

String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}

4.创建index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 3.0 MVC Series: Index - ViralPatel.net</title>
</head>
<body>
<a href="hello.html">Say Hello</a> //此处不能是hello,否则提示找不到。看下面的设置就明白了
</body>
</html>

5.创建view,在WEB_INFO下创建jsp文件夹,创建hello.jsp文件。//这里与ASP.NET MVC一样。视图的文件名与控制器相同

<html>
<head>
<title>Spring 3.0 MVC Series: Hello World - ViralPatel.net</title>
</head>
<body>
${message}
</body>
</html>

6.WEB配置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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

7.Spring配置文件 WEB-INF/spring-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan
base-package="net.viralpatel.spring3.controller" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" /> //设置控制器view文件夹的位置
<property name="suffix" value=".jsp" /> //视图文件的扩展名
</bean>
</beans>

8.启动,OK

基本上与ASP.NET MVC很像了。不知道有没有编辑的插件,就更好了。有兴趣的朋友到我的资源中下载源码.

你可能感兴趣的:(springMVC)