- 浏览: 62024 次
- 性别:
- 来自: 济南
文章分类
最新评论
-
fmpoffice:
不懂,但很想学习!!
菜鸟学习Granite Data Services 理解过的东西 -
bigbrother:
怎么没有Ext JS?
15 个 JavaScript Web UI 库 -
273289658:
哈哈 正在学Flex 想学它的好框架哦
个人感觉开发flex项目中比较好用的框架 -
过儿oO:
恩这个框架设计的非常不错
个人感觉开发flex项目中比较好用的框架 -
89756197:
正在看这个框架,谢谢
个人感觉开发flex项目中比较好用的框架
Create Hello World application in Spring 3.0 MVC
Welcome to the Part 2 of Spring 3.0 MVC Series. In previous article we went through the Introduction of Spring MVC 3.0 framework, its request processing lifecycle and architecture diagram. In this article, let us create a simple Hello World application in Spring MVC 3.0.
Spring 3.0 MVC Series
For creating the hello world demo application, we will use Eclipse IDE.
Things We Need
Before we starts with our first Hello World Struts 2 Example, we will need few tools.
- JDK 1.5 above (download)
- Tomcat 5.x above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (download)
- Eclipse 3.2.x above (download)
- Spring 3.0 MVC JAR files:(download). Following are the list of JAR files required for this application.
- commons-logging-1.0.4.jar
- jstl-1.2.jar
- org.springframework.asm-3.0.1.RELEASE-A.jar
- org.springframework.beans-3.0.1.RELEASE-A.jar
- org.springframework.context-3.0.1.RELEASE-A.jar
- org.springframework.core-3.0.1.RELEASE-A.jar
- org.springframework.expression-3.0.1.RELEASE-A.jar
- org.springframework.web.servlet-3.0.1.RELEASE-A.jar
- org.springframework.web-3.0.1.RELEASE-A.jar
Note that depending on the current version of Spring MVC, the version number of above jar files may change.
Our Goal
Our goal is to create a basic Spring MVC application using latest 3.0 version. There will be an index page which will display a link “Say Hello” to user. On clicking this link, user will be redirected to another page hello which will display a message “Hello World, Spring 3.0!”.
Getting Started
Let us start with our first Spring 3.0 MVC based application.
Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.
After selecting Dynamic Web Project, press Next.
Write the name of the project. For example Spring3MVC. Once this is done, select the target runtime environment (e.g. Apache Tomcat v6.0). This is to run the project inside Eclipse environment. After this press Finish.
Once the project is created, you can see its structure in Project Explorer.
Now copy all the required JAR files in WebContent > WEB-INF > lib folder. Create this folder if it does not exists.
The Spring Controller Class
We will need a spring mvc controller class that will process the request and display a “Hello World” message. For this we will create a package net.viralpatel.spring3.controller
in the source folder. This package will contain the Controller file.
Create a class called HelloWorldController in net.viralpatel.spring3.controller package and copy following content into it.
File: net.viralpatel.spring3.controller.HelloWorldController
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
|
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" )
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!" ;
return new ModelAndView( "hello" , "message" , message);
}
} |
Note that we have annotated the HelloWorldController class with @Controller
and @RequestMapping("/hello")
on line 7 and 10. When Spring scans our package, it will recognize this bean as being a Controller bean for processing requests. The @RequestMapping
annotation tells Spring that this Controller should process all requests beginning with /hello in the URL path. That includes /hello/*
and /hello.html
.
The helloWorld() method returns ModelAndView
object. The ModelAndView object tries to resolve to a view named “hello” and the data model is being passed back to the browser so we can access the data within the JSP. The logical view name will resolve to "/WEB-INF/jsp/hello.jsp"
. We will discuss this shortly how the logical name “hello” which is return in ModelAndView object is mapped to path /WEB-INF/jsp/hello.jsp.
The ModelAndView object also contains a message with key “message” and value “Hello World, Spring 3.0!”. This is the data that we are passing to our view. Normally this will be a value object in form of java bean that will contain the data to be displayed on our view. Here we are simply passing a string.
The View: Create JSP
To display the hello world message we will create a JSP. Note that this JSP is created in folder /WEB-INF/jsp. Create hello.jsp
under WEB-INF/jsp directory and copy following content into it.
File: WEB-INF/jsp/hello.jsp
1
2
3
4
5
6
7
8
|
< html >
< head >
< title >Spring 3.0 MVC Series: Hello World - ViralPatel.net</ title >
</ head >
< body >
${message}
</ body >
</ html >
|
The above JSP simply display a message using expression ${message}
. Note that the name “message” is the one which we have set in ModelAndView
object with the message string.
Also we will need an index.jsp file which will be the entry point of our application. Create a file index.jsp under WebContent folder in your project and copy following content into it.
File: WebContent/index.jsp
1
2
3
4
5
6
7
8
|
< html >
< head >
< title >Spring 3.0 MVC Series: Index - ViralPatel.net</ title >
</ head >
< body >
< a href = "hello.html" >Say Hello</ a >
</ body >
</ html >
|
Mapping Spring MVC in WEB.xml
As discussed in the previous article (Introduction to Spring 3.0 MVC), the entry point of Spring MVC application will be the Servlet define in deployment descriptor (web.xml). Hence we will define an entry of org.springframework.web.servlet.DispatcherServlet
class in web.xml.
Open web.xml file which is under WEB-INF folder and copy paste following code.
File: WEB-INF/web.xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<? 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 >
|
The above code in web.xml will map DispatcherServlet with url pattern *.html. Also note that we have define index.jsp as welcome file.
One thing to note here is the name of servlet in <servlet-name> tag in web.xml. Once the DispatcherServlet is initialized, it will looks for a file name [servlet-name]-servlet.xml
in WEB-INF folder of web application. In this example, the framework will look for file called spring-servlet.xml
.
Spring configuration file
Create a file spring-servlet.xml in WEB-INF folder and copy following content into it.
File: WEB-INF/spring-servlet.xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
|
<? 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/" />
< property name = "suffix" value = ".jsp" />
</ bean >
</ beans >
|
In the above xml configuration file, we have defined a tag <context:component-scan>. This will allow Spring to load all the components from package net.viralpatel.spring3.controller
and all its child packages. This will load our HelloWorldController
class. Also we have defined a bean viewResolver
. This bean will resolve the view and add prefix string /WEB-INF/jsp/ and suffix .jsp to the view in ModelAndView. Note that in our HelloWorldController class, we have return a ModelAndView object with view name “hello”. This will be resolved to path /WEB-INF/jsp/hello.jsp.
That’s All Folks
You may want to run the application now and see the result. I assume you have already configured Tomcat in eclipse. All you need to do:
Open Server view from Windows > Show View > Server. Right click in this view and select New > Server and add your server details.
To run the project, right click on Project name from Project Explorer and select Run as > Run on Server (Shortcut: Alt+Shift+X, R)
Download Source Code
Click here to download source code (9.05kb)
Moving On
In this tutorial we created a small Hello World application using Spring 3.0 MVC framework. Also we learned about the spring configuration and different annotations like @Controller
and @RequestMapping
. In next article we will see how easy it is to handle form data using Spring 3.0 MVC.
发表评论
-
深入浅出REST
2011-10-13 15:37 691写道 http://www.infoq.com/cn/art ... -
理解RESTful架构
2011-10-11 14:35 706越来越多的人开始意 ... -
json 介绍
2011-10-11 10:23 565具体看附件 -
Java 的JSON、XML转换方法
2011-10-11 10:05 1327Java 的JSON、XML转换方法 JSON及XML的 ... -
htmlParse 学习小结
2011-03-16 16:39 850htmlparser是个优秀的网页 ... -
Framework view的假想
2010-11-04 10:12 588架构是系统如何建构的决策. 软件架构是设计的一部分,但不是全 ... -
获得java类所在路径下的某个文件的内容
2009-06-29 16:43 1368获得某个java类所在工程中的目录下的某个文件内容 pub ... -
做过的项目中常用的sql语句
2009-01-08 11:02 728select count(*) from 表名 where ... -
把Map对象中的值拷贝给一个实体bean
2009-01-06 11:56 3119下面的知识一个代码片断,最重要的就是那段循环代码,可以根据具体 ... -
axis.jar的应用
2009-01-06 11:36 1691在项目中应用到的一个axis.jar包,能从控制台中监测到页面 ... -
利用服务器端方法调用访问链接返回结果的一个方法
2009-01-06 10:52 820调用服务器端方法时,根据服务器端方法访问另一个链接后返回的结果 ...
相关推荐
2. **Part 2: Create Hello World Application in Spring 3.0 MVC** - 通过创建一个简单的Hello World应用,读者将学会如何配置Spring MVC环境并处理第一个HTTP请求。 3. **Part 3: Handling Forms in Spring 3.0 ...
通过命令行创建新应用,如`grails create-app HelloWorld`,然后创建控制器并运行`grails run-app`,可以在浏览器中查看HelloWorld应用。Grails支持IDE集成,如Groovy-Grails Tool Suite (GGTS),提供类似Eclipse的...
Indeed, the fact that a simple Spring Boot Hello World application can fit into a tweet is a radical departure from what the same functionality required on the vm only a few short years ago. Out-of-...
#### 一、Spring Web MVC 注解 ##### 1. `@RequestMapping` `@RequestMapping` 是 Spring MVC 中的核心注解之一,用于映射 HTTP 请求到处理该请求的具体方法上。该注解可以作用于类级别或方法级别。 - **属性介绍...
在`pom.xml`中添加`spring-boot-starter-webflux`依赖,并在`application.properties`中进行必要的配置。然后,可以定义一个`Controller`类,使用`@RestController`注解标记,以表明这是一个处理HTTP请求的组件。 ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
这个注解会启动 Spring 的自动配置、Spring Boot 的特性以及 Spring MVC,以便处理 HTTP 请求。例如: ```java @SpringBootApplication public class Application { public static void main(String[] args) { ...
return "Hello, World!"; } } ``` #### 三、@RequestMapping `@RequestMapping` 是Spring MVC中最常用的注解之一,用于映射HTTP请求到具体的处理器方法上。它可以应用于类级别或者方法级别。当应用在类上时,...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
<h1 th:text="#{hello.message}">Hello, World! ``` ### 四、数据库集成 Spring Boot支持多种数据库,如MySQL、PostgreSQL等。只需添加对应的数据库驱动依赖,配置数据源,即可自动配置JdbcTemplate或JPA。 ``...
添加一个简单的`main`方法并运行,就可以看到"Hello, World!"式的输出。 至此,你已经成功创建了一个基本的Spring Boot项目。随着对Spring Boot的理解加深,你可以逐步添加更多的功能,如数据库连接、RESTful API、...
return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(SampleController.class, args); } } ``` **方案二:使用Spring Initializr** 另一种更快速的方法是使用...
例如,`<allow>`标签内的`<create>`元素定义了一个名为`SelectSys`的JavaScript对象,它通过`creator="spring"`与Spring框架集成,并且`scope="application"`表示这个对象是全局的。`<param>`元素用于设置bean的名称...
如果使用的是Spring Boot,可以集成Spring MVC和Spring Web的REST功能,简化配置和开发过程。 在REST服务中,数据通常以JSON(JavaScript Object Notation)格式交换。Java中的Jackson库可以帮助我们将Java对象序列...