Spring MVC Hello World 例子
来源:互联网 发布:大掌柜软件免费版 编辑:程序博客网 时间:2024/06/05 16:40
下面的例子说明了如何使用 Spring MVC 框架来编写一个简单的基于 web 的 Hello World 应用程序。为了开始使用它,让我们在恰当的位置使用 Eclipse IDE,然后按照下面的步骤使用 Spring 的 Web 框架来开发一个动态 Web 应用程序:
这里是 HelloController.java 文件的内容:
package com.tutorialspoint;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.ui.ModelMap;@Controller@RequestMapping("/hello")public class HelloController{ @RequestMapping(method = RequestMethod.GET) public String printHello(ModelMap model) { model.addAttribute("message", "Hello Spring MVC Framework!"); return "hello"; }}
下面是 Spring Web 配置文件 web.xml 的内容
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC Application</display-name> <servlet> <servlet-name>HelloWeb</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HelloWeb</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>
下面是另一个 Spring Web 配置文件 HelloWeb-servlet.xml 的内容
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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"> <context:component-scan base-package="com.tutorialspoint" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean></beans>
下面是 Spring 视图文件 hello.jsp 的内容
<%@ page contentType="text/html; charset=UTF-8" %><html><head><title>Hello World</title></head><body> <h2>${message}</h2></body></html>
最后,下面是包含在你的 web 应用程序中的 Spring 和其他库。你仅仅需要将这些文件拖拽到 WebContent/WEB-INF/lib 文件夹中。
commons-logging-x.y.z.jar
org.springframework.asm-x.y.z.jar
org.springframework.beans-x.y.z.jar
org.springframework.context-x.y.z.jar
org.springframework.core-x.y.z.jar
org.springframework.expression-x.y.z.jar
org.springframework.web.servlet-x.y.z.jar
org.springframework.web-x.y.z.jar
- spring-web.jar
一旦你完成了创建源代码和配置文件后,导出你的应用程序。右键单击你的应用程序,并且使用 Export > WAR File选项,并且在 Tomcat 的 webapps 文件夹中保存你的 HelloWeb.war 文件。
现在启动你的 Tomcat 服务器,并且确保你能够使用标准的浏览器访问 webapps 文件夹中的其他 web 页面。现在尝试访问该 URL http://localhost:8080/HelloWeb/ 访问相同的页面,但是建议在不同的文件夹下有不同的功能。
from: http://wiki.jikexueyuan.com/project/spring/mvc-framework/spring-mvc-hello-world-example.html
- Spring MVC Hello World 例子
- spring mvc Hello World 的例子
- Spring MVC hello world例子程序
- spring mvc Hello World
- Spring MVC hello world!
- 基于Spring MVC的Hello World例子教程
- Hello World Spring MVC Hello World
- Spring MVC Hello world教程 Spring MVC Hello World Example
- Spring MVC Hello World Example
- Spring MVC Hello World Example
- Spring MVC Hello World 实例
- Spring MVC hello world example
- Spring MVC hello world example
- Spring MVC hello world example
- Spring MVC之hello world
- Spring mvc ---Hello World入门
- Spring MVC hello world example
- Spring MVC Hello World Example
- 一个小片段来看spring test中的链式调用
- 设计模式六大原则(4)接口隔离原则(Interface Segregation Principle)
- opencv Mat常用操作
- 剑指offer - 数字在排序数组中出现的次数
- Android开发之使用DrawerLayout实现侧拉菜单功能
- Spring MVC Hello World 例子
- Linux shell脚本:数学运算的三种方式
- 联想小新V3000安装windows7系统和关闭FN键
- POJ 1704 Georgia and Bob 博弈
- Android 编译环境的搭建
- UI高级------多线程(线程与进程)
- Arrays源码分析
- 多功能彩屏ACSII 8*16 ,6*8实用显示程序
- Bulls and Cows