SpringMVC实例之HelloWorld
来源:互联网 发布:淘宝云客服报名条件 编辑:程序博客网 时间:2024/06/05 09:47
本文介绍简单的SpringMVC的demo
步骤一:添加框架jar包
在myesclipse上创建Web project,并添加相应jar包,如图:
步骤二:在web.xml配置DispatcherServlet
DispatcherServlet为SpringMVC的前端控制器,它负责接收请求并将请求分发到对应的控制器:
<?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" 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"> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <!-- 配置springMVC配置文件的名称和位置,若不设置本节点, 则会使用默认配置文件/WEB-INF/<servlet-name>-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>
步骤三:利用注解创建控制器
package test.handlers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/* * */@Controllerpublic class HelloController { /** * 1. 使用 @RequestMapping 注解来映射请求的URL * 2. 返回视图会通过视图解析器解析为实际的物理视图,对于 InternalResourceViewResolver 视图解析器, 会做如下解析 * 通过 prefix + returnVal + suffix 这样的方式得到实际的物理视图,然后做转发操作 * 如本例中的: WEB-INF/view/success.jsp * @return */ @RequestMapping("/helloworld") public String hello(){ System.out.print("hello!"); return "success"; }}
步骤四:创建要跳转的页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP 'success.jsp' starting page</title> </head> <body> Hello World <br> </body></html>
步骤五:创建springmvc-servlet.xml,并设置自动扫描包及视图解析器
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="test"></context:component-scan> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="WEB-INF/view/"></property> <property name="suffix" value=".jsp"></property> </bean></beans>
0 0
- SpringMVC实例之HelloWorld
- 【springmvc】 springmvc 之 HelloWorld
- springmvc之HelloWorld
- 剖析springmvc之HelloWorld
- springmvc之helloworld
- SpringMVC之HelloWorld
- SpringMVC之“HelloWorld”起步
- springmvc之helloworld
- SpringMVC之HelloWorld。
- SpringMVC框架之HelloWorld
- 第一节:springmvc的helloworld实例
- SpringMVC学习简单HelloWorld实例
- 基于springMVC的HelloWorld实例
- 基于springMVC的HelloWorld实例
- hadoop实例之HELLOWORLD
- hadoop实例之HELLOWORLD
- Maven+SpringMVC整合之helloworld
- Maven+SpringMVC整合之helloworld
- 为什么要清除浮动
- Angular运行于nw.js时ng-src无法显示的问题解决
- 图解sendRedirect,include,forward的区别
- Matlab中的括号()[]{}
- 面对对象编程总结(一)
- SpringMVC实例之HelloWorld
- 并发学习3-启动多个线程
- 初始化与赋值之间的区别
- 堆排序
- em unit in font-size
- window.onload和$(document).ready()的区别
- 含有无符号类型的表达式
- StringTokenizerDemo
- 开始博客生涯