你的位置:首页 > Java教程

[Java教程]SpringMVC的注解开发入门


1.Spring MVC框架简介

  • 支持REST风格的URL
  • 添加更多注解,可完全注解驱动
  • 引入HTTP输入输出转换器(HttpMessageConverter)
  • 和数据转换、格式化、验证框架无缝集成
  • 对静态资源处理提供特殊支持
  • 更加灵活的控制器方法签名,可完全独立于Servlet API

 

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

Spring MVC 3框架的默认实现者

 

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

  1. 在应用中添加Spring框架支持;
  2. 在web.
  3. 编写Spring-MVC的配置文件
  4. 将任意JavaBean通过注解配置成Controller(控制器)并注解其中的方法
  5. 完成

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

   一:转发(forward)

 

源码介绍:

1.lib包(jar包)和 web.

<??><web-app ="http://www.w3.org/2001/ ="http://java.sun.com/ ="http://java.sun.com/ xsi:schemaLocation="http://java.sun.com/ version="2.5"> <display-name></display-name> <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:applicationContext.</param-value>  </init-param>  <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping>  <servlet-name>springmvc</servlet-name>  <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>

View Code

2.index.jsp(登录主页)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%  String path = request.getContextPath();  String basePath = request.getScheme() + "://"      + request.getServerName() + ":" + request.getServerPort()      + path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>登录主页</title></head><body>  <form action="frist.do" method="post">    用户名:<input type="text" name="name" /> 年龄:<input type="text"      name="age" /> <input type="submit" value="提交" />  </form></body></html>

View Code

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

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head>  <base href="<%=basePath%>">  <title>成功欢迎页面</title> </head> <body> 欢迎您<br/>  姓名:${name}<br/>  年龄:${age } </body></html>

View Code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%  String path = request.getContextPath();  String basePath = request.getScheme() + "://"      + request.getServerName() + ":" + request.getServerPort()      + path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>错误页面</title></head><body>这是错误页面</body></html>

View Code

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

package cn.zhang.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;//定义自己的处理器@Controllerpublic class MyController{  @RequestMapping(value="/frist.do",produces="text/html;charset=utf-8",method=RequestMethod.POST)  public String frist(Model model,String name,int age){    model.addAttribute("name", name);    model.addAttribute("age", age);    System.out.println(name);    System.out.println(age);    return "forward:/hello.jsp";  }}

View Code

5.applicationContext.

<??><beans ="http://www.springframework.org/schema/beans"  ="http://www.w3.org/2001/   ="http://www.springframework.org/schema/context"  ="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.xsd    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">    <!-- 使用注解方式完成映射 -->  <context:component-scan base-package="cn.zhang.controller"></context:component-scan>  <!-- mvc的注解驱动 -->  <mvc:annotation-driven/>  </beans>

View Code

 效果展示:

1.输入成功的情况

点击提交跳到成功页面:

 

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

 

   二:重定向(redirect)

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

package cn.zhang.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;//定义自己的处理器@Controllerpublic class MyController{    @RequestMapping(value="/frist.do")//登录请求的是frist.do  public String frist(Model model,String name,int age){    return "redirect:hello.do";//重定向到hello.do  }    @RequestMapping(value="/hello.do")  public String hello(Model model,String name,int age){    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