SpringMVC-03_SpringMVC基于注解开发

来源:互联网 发布:java main启动项目 编辑:程序博客网 时间:2024/06/18 00:13

第一篇,小编给大家分享了,使用配置文件的方式,实现Hello SpringMVC,本篇小编讲给大家分享SpringMVC基于注解的开发

其实,SpringMVC基于注解的开发比配置文件的方式开发,简单的多,所以现在很多企业都在采用基于注解开发,

工程目录结构: 和第一篇的一样


和第一篇工程的不同之处就是:springmvc-servlet.xml和TestController不同

代码:

package com.snail.controller;import java.text.SimpleDateFormat;import java.util.Date;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.propertyeditors.CustomBooleanEditor;import org.springframework.beans.propertyeditors.CustomDateEditor;import org.springframework.format.datetime.DateFormatter;import org.springframework.stereotype.Controller;import org.springframework.web.bind.ServletRequestDataBinder;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.RequestMapping;import com.snail.entity.Person;@Controller// 表示是一个controller控制器类@RequestMapping("/test")//表示命名空间public class TestController {@RequestMapping("/hello.do")//表示将在页面上以:http://localhost:8080/test/hello.do访问这个方法public String hello() {System.out.println("hello springmvc2");return "index";}//得到页面上的请求参数,方式1(通过HttpServletRequest得到值)@RequestMapping("/hello2.do")public String hello2(HttpServletRequest request) {// 得到请求参数String name = request.getParameter("name");System.out.println(name);return "index";}//得到页面上的请求参数,方式2(直接定义参数,参数的名字为页面上参数的名字)@RequestMapping("/hello3.do")public String hello3(String name) {System.out.println(name);return "index";}//接受页面上,Date类型的数据,主要需要注册时间类型的转换器@RequestMapping("/hello4.do")public String hello4(String name, Date birthday) {System.out.println(name + " " + (birthday.getYear() + 1900) + "-"+ (birthday.getMonth() + 1) + "-" + birthday.getDate());return "index";}// 注册时间类型的转换器@InitBinderpublic void initBinder(ServletRequestDataBinder binder) {binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));}//使用实体对象,接受页面上传过来的值,当然:参数为实体对象中的字段的名字@RequestMapping("hello5.do")public String hello5(Person person){System.out.println(person);return "index";}//接受页面上为数组类型的值,eg:爱好@RequestMapping("hello6.do")public String hello6(String name[]){for (String string : name) {System.out.println(string);}return "index";}}

配置文件:springmvc-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd"><!-- 扫描仪:base-package="com.snail.controller"为扫描那个包下面的Java类 --><context:component-scan base-package="com.snail.controller"></context:component-scan><!--配置ViewResolver --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 配置前缀 --><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean></beans>




0 0
原创粉丝点击