springmvc框架知识点

SpringMvc

简单来说,springMvc就是Servlet,用来接收前台信息,天生与spring是一对,能很好的兼容。

框架图

在这里插入图片描述
Controller:处理器/页面控制器,做的是MVC中的C的事情,但控制逻辑转移到前端控制器了,用于对请求进行处理;

HandlerMapping:请求到处理器的映射,如果映射成功返回一个HandlerExecutionChain对象(包含一个Handler处理器(页面控制器)对象、多个HandlerInterceptor拦截器)对象;如BeanNameUrlHandlerMapping将URL与Bean名字映射,映射成功的Bean就是此处的处理器;

HandlerAdapter:HandlerAdapter将会把处理器包装为适配器,从而支持多种类型的处理器,即适配器模式的应用,从而很容易支持很多类型的处理器;如SimpleControllerHandlerAdapter将对实现了Controller接口的Bean进行适配,并且调处理器的handleRequest方法进行功能处理;

ViewResolver:ViewResolver将把逻辑视图名解析为具体的View,通过这种策略模式,很容易更换其他视图技术;如InternalResourceViewResolver将逻辑视图名映射为jsp视图;

LocalResover:本地化解析,因为Spring支持国际化,因此LocalResover解析客户端的Locale信息从而方便进行国际化;

ThemeResovler:主题解析,通过它来实现一个页面多套风格,即常见的类似于如软件皮肤效果;

MultipartResolver:文件上传解析,用于支持文件上传;

HandlerExceptionResolver:处理器异常解析,可以将异常映射到相应的统一错误界面,从而显示用户友好的界面(而不是给用户看到具体的错误信息);

RequestToViewNameTranslator:当处理器没有返回逻辑视图名等相关信息时,自动将请求URL映射为逻辑视图名;

FlashMapManager:用于管理FlashMap的策略接口,FlashMap用于存储一个请求的输出,当进入另一个请求时作为该请求的输入,通常用于重定向场景。

下面我们简单写一个小例子

导包不能忘,代码状又强

HelloController.java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloController implements Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		ModelAndView mav=new ModelAndView();
		mav.setViewName("Hello1.jsp");		
		return mav;
	}

}

SpringMVC.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" 
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-4.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<!--  上边多了XMLNS:MVC 下边多了两行mvc地址-->

<!-- /hello.action定义 action -->
<bean name="/hello.action" class="com.neusoft.controller.HelloController"></bean>


<!-- 配置扫描包 -->
<!-- <context:component-scan base-package="com.easy.controller"></context:component-scan> -->

<!-- 配置处理器映射器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> -->
<!-- 配置处理器适配器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> -->
<!-- 注解驱动 -->
<mvc:annotation-driven />
<!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> 
		"/WEB-INF/jsp/test.jsp" -->
	<!-- 配置视图解析器 -->
<!-- 	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		配置逻辑视图的前缀
		 <property name="prefix" value="/admin/product/" /> 
		配置逻辑视图的后缀
		<property name="suffix" value=".jsp" />
	</bean> -->




</beans>

web.xml

<?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_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Test0928springmvcTest</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>
  
  <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:SpringMVC.xml</param-value>
  </init-param>
  </servlet>
  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <!--/是除了jsp都通过  *.action是含有action的都经过  -->
  <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

Hello.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
Hello1
</body>
</html>

将项目发布到Tomcat中,地址填写你配置的action,就会自动跳转到jsp界面 这就是一个简单的springmvc工作原理。

当然还有更简单的注解方式

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Hello4Controller {
//此处是设置的路径名
	@RequestMapping("bbb.action")
	public ModelAndView tiaozhuan() {
		// TODO Auto-generated method stub
		ModelAndView mav=new ModelAndView();
		mav.setViewName("Hello1.jsp");		
		return mav;
	}
	@RequestMapping("ccc.action")
	public ModelAndView tiaozhuan2() {
		// TODO Auto-generated method stub
		ModelAndView mav=new ModelAndView();
		mav.setViewName("Hello2.jsp");		
		return mav;
	}
}

猜你喜欢

转载自blog.csdn.net/ZhouhbUp/article/details/82884564
今日推荐