您的位置:首页 > 编程语言 > Java开发

Spring MVC Hello World 实例

2015-01-07 23:59 323 查看
下面的例子说明了如何使用Spring MVC框架基础编写一个简单的web Hello World应用程序。要开始使用它,我们使用 Eclipse IDE工具,并按照以下步骤使用Spring Web框架开发动态Web应用程序:
步骤描述
1Create a Dynamic Web Project with a name HelloWeb and create a packagecom.yiibai under the src folder
in the created project.
2Drag and drop below mentioned Spring and other libraries into the folder WebContent/WEB-INF/lib.
3Create a Java class HelloController under the com.yiibai package.
4Create Spring configuration files Web.xml and HelloWeb-servlet.xml under theWebContent/WEB-INF folder.
5Create a sub-folder with a name jsp under the WebContent/WEB-INF folder. Create a view filehello.jsp under this sub-folder.
6The final step is to create the content of all the source and configuration files and export the application as explained below.
这里是HelloController.java文件的内容:
package com.yiibai;

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.yiibai" />

<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>


最后,下面是Spring和其他库包含在您的Web应用程序。只需将这些文件拖放它们到 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文件选项并保存HelloWeb.war 文件到Tomcat 的 webapps文件夹中。

现在启动Tomcat服务器,并确保您能够访问webapps文件夹,使用标准的浏览器。现在尝试访问该URL http://localhost:8080/HelloWeb/hello,如果Spring的Web应用程序没有问题,应该看到以下结果:


你要注意,在给定的URL,是HelloWeb应用程序名称 hello是我们刚才在我们的控制器使用使用@RequestMapping(“/hello”)虚拟子文件夹。您可以使用root直接在使用使用@RequestMapping(“/”)映射网址,在这种情况下,可以使用短网址http://localhost:8080/HelloWeb/访问相同的页面,但它建议有不同功能文件夹。

文献摘录于 http://www.yiibai.com/ , 如有问题,请联系 QQ:1519891098 , 我很愿意和您沟通!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: