SpringMVC创建一个HelloWorld程序
来源:互联网 发布:淘宝秒杀群运营系统 编辑:程序博客网 时间:2024/05/16 23:34
原文:http://www.admin10000.com/document/6436.html
1.导入架包(不同的版本架包可能会出现异常)
本例子采用4.1.6版本
2.添加Web.xml配置文件中关于SpringMVC的配置
<!--configure the setting of springmvcDispatcherServlet and configure the mapping--><servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!-- <load-on-startup>1</load-on-startup> --></servlet><servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern></servlet-mapping>
3.在src下添加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.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- scan the package and the sub package --> <context:component-scan base-package="test.SpringMVC"/> <!-- don't handle the static resource --> <mvc:default-servlet-handler /> <!-- if you use annotation you must configure following setting --> <mvc:annotation-driven /> <!-- configure the InternalResourceViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean>
4.在WEB-INF文件夹下创建名为jsp的文件夹,用来存放jsp视图。创建一个hello.jsp,在body中添加“Hello World”。
5.建立包及Controller,如下所示
6.编写Controller代码
@Controller@RequestMapping("/mvc")public class mvcController { @RequestMapping("/hello") public String hello(){ return "hello"; }}
7.启动服务器,键入 http://localhost:8080/项目名/mvc/hello
二、配置解析
1.Dispatcherservlet
DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据相应的规则分发到目标Controller来处理,是配置spring MVC的第一步。
2.InternalResourceViewResolver
视图名称解析器
3.以上出现的注解
@Controller 负责注册一个bean 到spring 上下文中
@RequestMapping 注解为控制器指定可以处理哪些 URL 请求
三、SpringMVC常用注解
@Controller
负责注册一个bean 到spring 上下文中
@RequestMapping
注解为控制器指定可以处理哪些 URL 请求
@RequestBody
该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上 ,再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上
@ResponseBody
该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区
@ModelAttribute
在方法定义上使用 @ModelAttribute 注解:Spring MVC 在调用目标处理方法前,会先逐个调用在方法级上标注了@ModelAttribute 的方法
在方法的入参前使用 @ModelAttribute 注解:可以从隐含对象中获取隐含的模型数据中获取对象,再将请求参数 –绑定到对象中,再传入入参将方法入参对象添加到模型中
@RequestParam
在处理方法入参处使用 @RequestParam 可以把请求参 数传递给请求方法
@PathVariable
绑定 URL 占位符到入参
@ExceptionHandler
注解到方法上,出现异常时会执行该方法
@ControllerAdvice
使一个Contoller成为全局的异常处理类,类中用@ExceptionHandler方法注解的方法可以处理所有Controller发生的异常
- SpringMVC创建一个HelloWorld程序
- SpringMVC基础入门,创建一个HelloWorld程序
- SpringMVC基础入门--创建一个HelloWorld程序
- SpringMVC基础入门,创建一个HelloWorld程序
- SpringMVC基础入门,创建一个HelloWorld程序
- SpringMVC创建HelloWorld程序
- 一、SpringMVC基础入门,创建一个HelloWorld程序
- 一、SpringMVC基础入门,创建一个HelloWorld程序
- 一、SpringMVC基础入门,创建一个HelloWorld程序
- SpringMVC学习笔记---- 一、SpringMVC基础入门,创建一个HelloWorld程序
- 1.3创建一个“HelloWorld"程序
- SpringMVC---HelloWorld程序的创建步骤
- 创建一个helloworld springmvc的web工程
- 创建一个Spring的HelloWorld程序
- springMVC + maven helloworld程序
- springMVC 入门程序 helloworld!
- springMVC入门程序helloworld
- 在eclipse下创建一个helloworld小程序
- Fibonacci数列(矩阵乘法快速幂)
- Intel SGX 技术初探
- 第三章 例题7(乒乓比赛)基础树状数组
- android手机的传感器调用
- Objective-C之定位&地图
- SpringMVC创建一个HelloWorld程序
- PAT(A) - 1076. Forwards on Weibo (30)
- Zephys OS nano 内核篇:task 服务 - 基础
- 初试cnn(1)
- 第7届山东省赛sdut 3566---Triple Nim
- 如何使用redis做mysql的缓存
- 深入理解Java:SimpleDateFormat安全的时间格式化
- Zephys OS nano 内核篇:fiber 服务 - 基础
- 9个完整android开源app项目