Spring MVC Hello World 例子


当前第2页 返回上一页

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/hello。如果你的 Spring Web 应用程序一切都正常,你应该看到下面的结果:

你应该注意,在给定的 URL 中,HelloWeb 是这个应用程序的名称,并且 hello 是我们在控制器中使用 @RequestMapping("/hello") 提到的虚拟子文件夹。当使用 @RequestMapping("/") 映射你的 URL 时,你可以使用直接 root,在这种情况下,你可以使用短 URL http://localhost:8080/HelloWeb/ 访问相同的页面,但是建议在不同的文件夹下有不同的功能。



标签:Spring IoC

返回前面的内容

相关阅读 >>

spring 声明式事务管理

spring jsr-250 注释

spring 自动装配 byname

spring 中基于 aop 的 @aspectj

spring bean 后置处理器

spring mvc hello world 例子

spring 中的事件处理

spring 编程式事务管理

spring 页面重定向例子

spring 中 sql 的存储过程

更多相关阅读请进入《Spring IoC》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    正在狠努力加载,请稍候...