zoukankan      html  css  js  c++  java
  • Spring MVC(二)基于标注的MVC

    1.基于标注的Spring MVC
    1.1 建立一个项目导入jar包(ioc aop mvc) 拷贝容器对应的配置文件到src下
    在WEB-INF建立一个login.jsp
    1.2 在web.xml 配置一个DispatcherServlet 并且使用
    初始化参数 contextConfigLocation 关联容器对应的配置文件
    1.3 开启基于标注 Spring MVC
    开启组件扫描 <context:component-scan base-package="" />
    开启mvc的标注 <mvc:annotation-driven /> 自动配置了一个HandlerMapping
    1.4 写一个普通的java类使用@Controller 把java类变成控制器
    控制器方法的返回值可以是String也可以是 ModelAndView
    方法名任意参数任意
    在控制器方法上加 @RequestMapping("/请求路径")
    1.5 配置视图处理器

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:lang="http://www.springframework.org/schema/lang"
     6     xmlns:mvc="http://www.springframework.org/schema/mvc"
     7     xmlns:util="http://www.springframework.org/schema/util"
     8     xmlns:task="http://www.springframework.org/schema/task"
     9     xmlns:aop="http://www.springframework.org/schema/aop"
    10     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    11         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
    12         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    13         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    14         http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
    15         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    16         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
    17     <!-- 开启组件扫描 -->
    18     <context:component-scan base-package="com.xcz"></context:component-scan>
    19     <!-- 开启mvc标注 -->
    20     <mvc:annotation-driven></mvc:annotation-driven>
    21     <!-- 配置视图处理器 -->
    22     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    23         <property name="prefix" value="/WEB-INF/"></property>
    24         <property name="suffix" value=".jsp"></property>
    25     </bean>
    26 </beans>
    mvc.xml
     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <form action="" method="post">
    11         账号:<input type="text" value="请输入账号..."><br>
    12         密码:<input type="text" value="请输入密码..."><br>
    13         <input type="submit" value="登录"><br>
    14     </form>
    15 </body>
    16 </html>
    login.jsp
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
     3   <display-name>SpringMVC-03</display-name>
     4   <welcome-file-list>
     5     <welcome-file>index.html</welcome-file>
     6     <welcome-file>index.htm</welcome-file>
     7     <welcome-file>index.jsp</welcome-file>
     8     <welcome-file>default.html</welcome-file>
     9     <welcome-file>default.htm</welcome-file>
    10     <welcome-file>default.jsp</welcome-file>
    11   </welcome-file-list>
    12   <!-- 配置请求入口 -->
    13   <servlet>
    14       <servlet-name>SpringMVC</servlet-name>
    15     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    16     <!-- 配置初始化参数 -->
    17         <init-param>
    18             <param-name>contextConfigLocation</param-name>
    19             <param-value>classpath:mvc.xml</param-value>
    20         </init-param>
    21         <load-on-startup>1</load-on-startup>
    22     </servlet>
    23   <servlet-mapping>
    24       <servlet-name>SpringMVC</servlet-name>
    25       <url-pattern>*.do</url-pattern>
    26   </servlet-mapping>
    27 </web-app>
    web.xml
     1 package com.xcz.controller;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 
     6 @Controller
     7 public class LoginController {
     8     @RequestMapping("/login.do")
     9     public String Login() {
    10         return "login";
    11     }
    12 }
    controller

  • 相关阅读:
    CodeForces 510C Fox And Names (拓扑排序)
    Codeforces 1153D Serval and Rooted Tree (简单树形DP)
    HDU 6437 Problem L.Videos (最大费用)【费用流】
    Luogu P3381 (模板题) 最小费用最大流
    Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)
    Codeforces 1144F Graph Without Long Directed Paths (DFS染色+构造)
    HDU 2204 Eddy's 爱好 (容斥原理)
    Codeforces 939E Maximize! (三分 || 尺取)
    Codeforces 938D. Buy a Ticket (最短路+建图)
    CodeForces 959E Mahmoud and Ehab and the xor-MST (MST+找规律)
  • 原文地址:https://www.cnblogs.com/resultset/p/9938274.html
Copyright © 2011-2022 走看看