您的位置:首页 > 其它

Sping MVC-创建HelloWeb项目(一)

2013-09-26 11:21 375 查看
下面的例子显示怎样使用Spring MVC框架写一个简单的基于Web的应用程序,使用Eclipse
IDE作为开发工具,按照下面的步骤使用Spring Web框架来开发一个动态的Web应用

 
步骤
描述
1
创建一个Dynamic Web Project,命名为HelloWeb,在src文件夹下创建一个com.tutorialspoint的包。
2
将下面提到的Spring和其他库文件放到WebContent/WEB-INF/lib目录下
3
在com.tutorialspoint包下,创建一个类HelloController
4
在WebContent/WEB-INF目录下创建Spring的配置文件Web.xml和HelloWeb-servlet.xml
5
在WebContent/WEB-IN目录下创建一个子目录,命名为jsp,在jsp目录下创建一个视图文件hello.jsp
6
最后一步是创建源码和配置文件,以下面方式进行导出
 
这个是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>


 
最后,下面是需要用到的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选项并保存HelloWeb.war文件到
Tomcat下的 Webapps文件夹下
现在启动你的Tomcat服务器,确保你可以通过浏览器访问其他页面,现在试着访问这个URL
http://localhost:8080/HelloWeb/hello,如果看到下面的结果说明你成功了。
 



你应该注意一下URL,HelloWeb是应用的名称,hello是一个虚拟的子目录。在控制层使用@RequestMapping("/hello").你能够直接使用using
@RequestMapping("/")来映射根目录,但是强烈建议不同的目录代表不同的功能。
原文出自:http://www.tutorialspoint.com/spring/spring_mvc_hello_world_example.htm
 
 

 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: