SpringMVC的註解開發入門,springmvc註解入門

來源:互聯網
上載者:User

SpringMVC的註解開發入門,springmvc註解入門

1.Spring MVC架構簡介

  • 支援REST風格的URL
  • 添加更多註解,可完全註解驅動
  • 引入HTTP輸入輸出轉換器(HttpMessageConverter)
  • 和資料轉換、格式化、驗證架構無縫整合
  • 對靜態資源處理提供特殊支援
  • 更加靈活的控制器方法簽名,可完全獨立於Servlet API

 

2.Spring MVC架構結構,執行流程

 

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

4.今天我們先來瞭解一下我們註解開發的小例子(簡單登入)

   一:轉寄(forward)

 

源碼介紹:

1.lib包(jar包)和 web.xml檔案的配置

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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.xml</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.xml(Spring的設定檔)

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.