成都创新互联网站制作重庆分公司

SpringMVCHelloWorld入门例子-创新互联

本例子为你讲解在spring3中如何使用基于注解的mvc框架.

例子中使用到的工具:
MyEclipse 9.1
jdk 1.6

1.添加Jar包引用
由于使用了Maven管理项目,所以,第一步就是添加引用.(没有使用Maven怎么办?那你直接下载Spring3的压缩包,添加相应的Jar文件就可以了。)

创新互联建站-成都网站建设公司,专注网站建设、做网站、网站营销推广,域名与空间,网页空间,网站改版维护有关企业网站制作方案、改版、费用等问题,请联系创新互联建站

Java代码  Spring MVC HelloWorld入门例子

  1.         org.springframework

  2.         spring-web

  3.         3.1.1.RELEASE

  4.         jar

  5.         compile

  6.     

  7.     

  8.         org.springframework

  9.         spring-core

  10.         3.1.1.RELEASE

  11.         jar

  12.         compile

  13.     

  14.     

  15.         org.springframework

  16.         spring-webmvc

  17.         3.1.1.RELEASE

  18.         jar

  19.         compile

  20.     

2.编写 Controller 和 Mapping
我们采用注解的方式配置,如果想使用XML的方式,可以查看文档,都是一样的配制方法.

Java代码  Spring MVC HelloWorld入门例子

  1. package com.vito.action;

  2. import org.springframework.stereotype.Controller;

  3. import org.springframework.ui.ModelMap;

  4. import org.springframework.web.bind.annotation.RequestMapping;

  5. import org.springframework.web.bind.annotation.RequestMethod;

  6. @Controller

  7. @RequestMapping("/welcome")

  8. public class HelloWorldController {

  9.     @RequestMapping(value="/hello",method = RequestMethod.GET)

  10.     public String printWelcome(ModelMap model) {

  11.         model.addAttribute("message", "Spring 3 MVC Hello World");

  12.         return "hello";

  13.     }

  14. }

3.JSP视图

Java代码  Spring MVC HelloWorld入门例子

  1.     

    Message : ${message}

4.Spring配置文件

Java代码  Spring MVC HelloWorld入门例子

  1.     xmlns:context="http://www.springframework.org/schema/context"

  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3.     xsi:schemaLocation="

  4.         http://www.springframework.org/schema/beans

  5.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

  6.         http://www.springframework.org/schema/context

  7.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  8.     

  9.     

  10.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">

  11.         

  12.                         

  13.             /WEB-INF/pages/

  14.         

  15.         

  16.             .jsp

  17.         

  18.     

5.web.xml

Java代码  Spring MVC HelloWorld入门例子

  1.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  2.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

  3.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  4.     

  5.         springMvc

  6.         org.springframework.web.servlet.DispatcherServlet

  7.         

  8.             contextConfigLocation

  9.             classpath:applicationContext.xml

  10.         

  11.     

  12.     

  13.         springMvc

  14.         /

  15.     

6.成果
访问:×××/technology
就可以得到这样的画面了:
Spring MVC HelloWorld入门例子

本站所有代码来源请查看:×××/technology

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页题目:SpringMVCHelloWorld入门例子-创新互联
本文链接:http://cxhlcq.com/article/ehisd.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部