GAE集成SpringMVC
程序员文章站
2022-03-24 17:21:26
...
摘要:
本教程,主要展示GAE如何集成Spring 3.0 MVC REST框架,同时,实现一个小案例。
所使用技术和工具:
- 1. Eclipse-Jee-Juno (version 4.2)
- 2. JDK 1.6
- 3. Google Plugin for Eclipse
- 4. Google App Engine Java SDK 1.7.4
- 5. Spring 3.1.0
备注: 在学习本教程之前,您可能需要了解SpringMVC开发技术,同样你也可能需要阅读下我上一篇博文 [GAE 教程] 初识GAE |
-
项目集成Spring 3.0 MVC REST 需要依赖以下jar
- 1. aopalliance-1.0.jar
-
2. commons-logging-1.1.1.jar
-
3. spring-aop-3.1.0.RELEASE.jar
-
4. spring-asm-3.1.0.RELEASE.jar
-
5. spring-beans-3.1.0.RELEASE.jar
-
6.spring-context-3.1.0.RELEASE.jar
-
7. spring-context-support-3.1.0.RELEASE.jar
-
8. spring-core-3.1.0.RELEASE.jar
-
9. spring-expression-3.1.0.RELEASE.jar
-
10. spring-web-3.1.0.RELEASE.jar
-
11. spring-webmvc-3.1.0.RELEASE.jar
- >>准备好Jar包以后,将这些jar包拷贝到项目的war/WEB-INF/lib 中
- >>导入后,通过Configure Build Path…配置引用下这些包就好了
-
>>配置SpringMVC,首先我们需要在war/WEB-INF/ 下创建一个Spring XML bean的配置文件 {YourServletName}-servlet.xml,用来定义bean 和 试图解析器
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 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
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <!-- 对web包中的所有类进行扫描,以完成bean的创建和自动依赖注入功能 -->
- <context:component-scan base-package="com.javawill" />
- <!-- 对模型视图的名称的解析 -->
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix">
- <value>/WEB-INF/jsp/</value>
- </property>
- <property name="suffix">
- <value>.jsp</value>
- </property>
- </bean>
- </beans>
-
>>同时,我们需要将Spring 集成到我们的web.xml中,有的人可能IDE无法显示web.xml的内容,只需要刷新web.xml 文件(注意是文件,不是内容)
- <?xml version="1.0" encoding="utf-8" standalone="no"?>
- <web-app xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <!-- spring mvc的servlet,加载WEB-INF/baifeibai-servlet.xml的配置文件,以启动Spring MVC模块 -->
- <servlet>
- <servlet-name>baifeibai</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>baifeibai</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/baifeibai-servlet.xml</param-value>
- </context-param>
- <!-- spring容器启动监听器 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>SystemServiceServlet</servlet-name>
- <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
- <init-param>
- <param-name>services</param-name>
- <param-value />
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>SystemServiceServlet</servlet-name>
- <url-pattern>/_ah/spi/*</url-pattern>
- </servlet-mapping>
- </web-app>
- >>现在我们看看我们项目的新结构,并实现一个简单的业务,通过Controller 定义一个变量值,并在JSP上展现出来
-
>>定义Controller.java
- /**
- * Create by Javawill in 2013-1-10
- */
- package com.javawill.example.spring.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- /**
- * GAE集成SpringMVC Controller 实例
- * @author <a href="mailto:iflytekwill@qq.com">Javawill</a>
- * @version 1.0
- */
- @Controller
- public class SpringController {
- @RequestMapping("/example/welcome.do")
- public String Welcome(Model model, String inputval){
- if ( inputval == null || ("".equals(inputval))) {
- inputval = "您可以试试在下面的输入框输入值,测试下Spring的传参!";
- } else {
- inputval = "您之前输入的值为:" + inputval;
- }
- model.addAttribute("hello", "您好,欢迎您来到GAE+SpringMVC学习项目演示网站!");
- model.addAttribute("msg", inputval);
- return "example/spring/welcome";
- }
- }
-
>>JSP页面代码
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- <!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=UTF-8">
- <title>Javawill 演示案例</title>
- </head>
- <body>
- <h2>${hello }</h2>
- ${msg }
- <form action="<c:url value='/example/welcome.do' />" method="post">
- 输入值:<input id="inputval" name="inputval" />
- <input type="submit" value="确定" />
- </form>
- </body>
- </html>
- >>接下来,我们远程部署到Google 云端,访问下 http://www.baifeibai.com
=================================================================== 本教程系列为本人写技术博客练笔之作,高手请无视,转载请点明出处!
===================================================================