java如何开发自己的注解_SpringMVC的注解开发入门

1.Spring MVC框架简介

支持REST风格的URL

添加更多注解,可完全注解驱动

引入HTTP输入输出转换器(HttpMessageConverter)

和数据转换、格式化、验证框架无缝集成

对静态资源处理提供特殊支持

更加灵活的控制器方法签名,可完全独立于Servlet API

2.Spring MVC框架结构,执行流程

cee3987129516eca167a34f6cde833e8.png

3.如何在应用中使用Spring-MVC?

在应用中添加Spring框架支持;

在web.xml中配置Spring-MVC的请求转发器(前端控制器)

编写Spring-MVC的配置文件

将任意JavaBean通过注解配置成Controller(控制器)并注解其中的方法

完成

4.今天我们先来了解一下我们注解开发的小例子(简单登录)

一:转发(forward)

8059a495a66840a517a5c3ae2fec8a21.png

源码介绍:

1.lib包(jar包)和 web.xml文件的配置

7bbed6f8529af9f94283e9e50db7d7a8.png

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

1

springmvc

*.do

index.jsp

View Code

2.index.jsp(登录主页)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

String basePath= request.getScheme() + "://"

+ request.getServerName() + ":" +request.getServerPort()+ path + "/";%>

登录主页用户名: 年龄:

View Code

3.hello.jsp(登录成功后跳到的页面)和 error.jsp(登录失败跳到的页面)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>

">

成功欢迎页面
姓名:${name}
年龄:${age }

View Code

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

Stringpath=request.getContextPath();StringbasePath=request.getScheme()+ "://"

+request.getServerName()+ ":" +request.getServerPort()+path+ "/";%>

">

错误页面

View Code

4.MyController.java(定义自己的处理器)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecn.zhang.controller;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;//定义自己的处理器

@Controllerpublic classMyController{

@RequestMapping(value="/frist.do",produces="text/html;charset=utf-8",method=RequestMethod.POST)public String frist(Model model,String name,intage){

model.addAttribute("name", name);

model.addAttribute("age", age);

System.out.println(name);

System.out.println(age);return "forward:/hello.jsp";

}

}

View Code

5.applicationContext.xml(Spring的配置文件)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

View Code

效果展示:

1.输入成功的情况

494232f7b717b0a8a79301b5eb1f9363.png

点击提交跳到成功页面:

2756606236e1e47cc70e2db89196a653.png

如果输入其他的信息则会跳到错误页面:

054294126d03b8c32cfb24afa48044ab.png

二:重定向(redirect)

我们只要改我们的控制器就行了

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecn.zhang.controller;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;//定义自己的处理器

@Controllerpublic classMyController{

@RequestMapping(value="/frist.do")//登录请求的是frist.do

public String frist(Model model,String name,intage){return "redirect:hello.do";//重定向到hello.do

}

@RequestMapping(value="/hello.do")public String hello(Model model,String name,intage){

model.addAttribute("name", name);

model.addAttribute("age", age);

System.out.println(name);

System.out.println(age);if (name.equals("1")&&age==1) {return "redirect:/hello.jsp";//重定向到hello.jsp

}else{return "redirect:/error.jsp";//重定向到error.jsp

}

}

}

View Code

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值