一、SpringMVC概述
1.首先明白什么是SpringMVC?
SpringMVC 是一种基于 Java 的实现 MVC 设计模型的请求驱动类型的轻量级 Web 框架,属于 Spring FrameWork 的后续产品,已经融合在 Spring Web Flow 里面。Spring 框架提供了构建 Web 应用程序的全功 能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用 Spring 进行 WEB 开发时,可以选择使用 Spring 的 Spring MVC 框架或集成其他 MVC 开发框架,如 Struts1(现在一般不用),Struts2 等。 SpringMVC 已经成为目前最主流的 MVC 框架之一,并且随着 Spring3.0 的发布,全面超越 Struts2,成 为最优秀的 MVC 框架。 它通过一套注解,让一个简单的 Java 类成为处理请求的控制器,而无须实现任何接口。同时它还支持 RESTful 编程风格的请求。



<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<display-name>SpringMVC01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 前端控制器配置(DispatcherServlet) -->
<servlet>
<!-- 这个servlet-name的值和servlet-mapping里的servlet-name保持一致即可,
可以给任意值但要与servlet-mapping里的servlet-name保持一致 -->
<servlet-name>login</servlet-name>
<!-- 处理请求的逻辑类 这个类是固定的,也就是说<servlet-class>
org.springframework.web.servlet.DispatcherServlet</servlet-class>
是固定不变的!! -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数:即加载我们的applicationContext.xml配置文件 那个param-name的值不用变 ,
需要改的只是那个配置文件名字<param-value>applicationContext.xml</param-value>,配置文件
名字是啥,这里的value就是啥 -->
<init-param>
<!-- <param-name>contextConfigLocation</param-name> 这个地方不用改,一直是这样!!! -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<!-- 接受请求的路径 -->
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
</web-app>
②applicationContext.xml配置
配置handlerMapping和ViewResolver
<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 配置handlerMapping:这个类在spring-webmvc包下的第四个包里的倒数第三个 -->
<bean id=" "
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!-- property:通过setMappings()方法注入数据,所以这里的<property name=" mappings">始终不用改变!! -->
<property name="mappings">
<!-- 集合注入 -->
<props>
<!-- 因为这个 HandlerMapping负责解析请求URL,所以key值为请求的url也就是web.xml中的<url-pattern>/login.do</url-pattern>中的/login.on。
请求到url后通过bean的id对应到Handler进行处理(这里的Handler一般为Controller里的一个方法method,也可以为servlet或者Controller等)-->
<prop key="/login.do">loginID</prop>
</props>
</property>
</bean>
<!-- 这里的bean的id和<prop key="/login.do">loginID</prop>里的loginID保持一致! -->
<bean id="loginID" class="com.albb.controller.Login"></bean>
<!-- ViewResolver配置 -->
<!-- 这个bean的class是固定的,不能变,一直是这一个! -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property><!-- 前缀 -->
<property name="suffix" value=".html"></property><!-- 后缀 -->
</bean>
</beans>
③ModelAndView配置
package com.albb.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;
public class Login implements Controller {
//配置controller组件,其实就是实现Controller接口
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
System.out.println("111");
return new ModelAndView("ok");
}
}
运行结果就是在控制台输出 111,跳转到success.jsp上并且输出!
做的不好还望批评指正!
SpringMVC框架详解
本文深入探讨SpringMVC框架,一种基于Java实现MVC设计模式的轻量级Web框架,介绍其架构、优势及入门配置,包括关键组件、执行过程和配置文件细节。
640

被折叠的 条评论
为什么被折叠?



