SpringMVC-03_SpringMVC基于注解开发

第一篇,小编给大家分享了,使用配置文件的方式,实现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";
	}
	// 注册时间类型的转换器
	@InitBinder
	public 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/beans
						http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context-3.0.xsd
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
						http://www.springframework.org/schema/aop
						http://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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android是一种基于Linux内核(不包含GNU组件)的自由及开放源代码的移动操作系统,主要应用于移动设备,如智能手机和平板电脑。该系统最初由安迪·鲁宾开发,后被Google公司收购并注资,随后与多家硬件制造商、软件开发商及电信营运商共同研发改良。 Android操作系统的特点包括: 开放源代码:Android系统采用开放源代码模式,允许开发者自由访问、修改和定制操作系统,这促进了技术的创新和发展,使得Android系统具有高度的灵活性和可定制性。 多任务处理:Android允许用户同时运行多个应用程序,并且可以轻松地在不同应用程序之间切换,提高了效率和便利性。 丰富的应用生态系统:Android系统拥有庞大的应用程序生态系统,用户可以从Google Play商店或其他第三方应用市场下载和安装各种各样的应用程序,满足各种需求。 可定制性:Android操作系统可以根据用户的个人喜好进行定制,用户可以更改主题、小部件和图标等,以使其界面更符合个人风格和偏好。 多种设备支持:Android操作系统可以运行在多种不同类型的设备上,包括手机、平板电脑、智能电视、汽车导航系统等。 此外,Android系统还有一些常见的问题,如应用崩溃、电池耗电过快、Wi-Fi连接问题、存储空间不足、更新问题等。针对这些问题,用户可以尝试一些基本的解决方法,如清除应用缓存和数据、降低屏幕亮度、关闭没有使用的连接和传感器、限制后台运行的应用、删除不需要的文件和应用等。 随着Android系统的不断发展,其功能和性能也在不断提升。例如,最新的Android版本引入了更多的安全性和隐私保护功能,以及更流畅的用户界面和更强大的性能。此外,Android系统也在不断探索新的应用场景,如智能家居、虚拟现实、人工智能等领域。 总之,Android系统是一种功能强大、灵活可定制、拥有丰富应用生态系统的移动操作系统,在全球范围内拥有广泛的用户基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值