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

猜你喜欢

转载自www.cnblogs.com/resultset/p/9938274.html