`
edwin492
  • 浏览: 112796 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

spring源码学习

    博客分类:
  • SSH
阅读更多

 

Jarorg.springframewrork.beansorg.springframework.beans.factoryBeanFactory类。BeanFactoryspring IOC的最基本的容器接口,其定义了对容器的一系列基本操作。如getBean(String name)返回容器中名namebean

IOC容器启动:

ContextLoaderListener继承ContextLoader类,实现ServletContextListener接口。ServletContextListener接口,它能够监听ServletContext对象的生命周期,实际上就是监听Web应用的生命周期。ContextLoaderListener就实现此监听接口,在contextInitialized方法中初始化应用容器,在contextDestroyed中关闭应用容器。ContextLoader类载入IOC容器作为根上下文存入ServletContext。此类主要包括对根上下文的初始化、关闭等相关操作。在创建WebApplicationContext时调用AbstractApplicationContext中的refresh()方法对WebApplicationContext进行初始化。

完成对ContextLoaderListener的初始化后,Tomcat会开始初始化DispatcherServletDispatcherServlet会建立自己的ApplicationContext,并以ServletContext中的根上下文作为自己的父上下文进行初始化,存入servletcontext中。其创建过程与创建根上下文类似。

       DispatcherServlet继承FrameworkServlet继承HttpServletBean继承HttpSevlet,故此该类也为一个Servlet类。springweb首先将传统的httpservlet抽象类包装成了beanframeworkservlet抽象出了web框架中的servlets的一些基本行为,比如对application context的访问;dispatcherservlet的主要工作就是将一个request分发到一个合适的处理器上,并将处理返回的modelandview绘制出来返回给客户端。FrameWorkServlet中的initServletBean调用DispatcherServlet中的onRefresh进行初始化,onRefresh调用initStrategies()方法对springMVC初始化HttpServletBean中的initServletBean()方法为空方法,其实现内容由其子类决定,此为templates的应用。(由具体类实现 ,子类可以改变实现内容,而不用修改操作流程。)

springmvc:

SpringMVC框架主要由DispatcherServlet、处理器映射、处理器、视图解析器、视图组成。

     整个处理过程从一个HTTP请求开始:

1DispatcherServlet接收到请求后,根据对应配置文件中配置的处理器映射,找到对应的处理器映射项(HandlerMapping),根据配置的映射规则,找到对应的处理器(Handler)。
2)调用相应处理器中的处理方法,处理该请求,处理器处理结束后会将一个ModelAndView类型的数据传给DispatcherServlet,这其中包含了处理结果的视图和视图中要使用的数据。
3DispatcherServlet根据得到的ModelAndView中的视图对象,找到一个合适的ViewResolver(视图解析器),根据视图解析器的配置,DispatcherServlet将视图要显示的数据传给对应的视图,最后给浏览器构造一个HTTP响应。
DispatcherServlet是整个Spring MVC的核心。它负责接收HTTP请求组织协调Spring MVC的各个组成部分。

 

 

@Controller
@RequestMapping("/login.action")
public class LoginAction {
	//@RequestMapping(params= "method=validate",method=RequestMethod.POST)
	@RequestMapping(params = "method=validate")
	public String login(@ModelAttribute("userBean") UserBean userBean, HttpServletRequest request,
			HttpServletResponse response) {
		if(!"admin".equals(userBean.getName())){
			
			return "index";
		}
		if(!"123456".equals(userBean.getPassword())){
			return "index";
		}
		return "WEB-INF/view/main";
	}
	@RequestMapping(params = "method=validate2")
	public String login2(@ModelAttribute("userBean") UserBean userBean, Model model) {
		if(!"admin".equals(userBean.getName())){
			model.addAttribute("msg", "用户名错误");//加入属性及值到model中
			return "index";
		}
		if(!"123456".equals(userBean.getPassword())){
			model.addAttribute("msg", "用户密码错误");
			return "index";
		}
		return "WEB-INF/view/main";
	}
	@RequestMapping(params = "method=validate3")
	public ModelAndView login3(@ModelAttribute("userBean") UserBean userBean, HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView model = new ModelAndView();
		model.addObject("userBean", userBean);
		if(!"admin".equals(userBean.getName())){
			model.setViewName("index");
			return model;
		}
		if(!"123456".equals(userBean.getPassword())){
			model.setViewName("index");
			return model;
		}
		model.setViewName("WEB-INF/view/main");
		return model;
	}
	/**note:
	 * import org.springframework.web.portlet.ModelAndView; 
	 *	不应该引入这个ModelAndView 应该引入: 
	 *	import org.springframework.web.servlet.ModelAndView; 
	 *  否则跳转的路径将不正确
	 */
}

 model

 

public class UserBean {
	private String name;
	private String password;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}

 

applictionContext:

 

<?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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
	<context:component-scan base-package="com.springmvc.action"/>
	
	<bean id="ViewResovler" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp"></property>
	</bean>
	
</beans>

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

 

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  ${userBean.name}
    <form action="<%=basePath%>login.action?method=validate" method="post">
    	username:<input type="text" name="name"/><br/>
    	password:<input type="text" name="password"/><br/>
    	<input type="submit" value="ok"/>
    </form>
    ${msg}
     <form action="<%=basePath%>login.action?method=validate2" method="post">
    	username:<input type="text" name="name"/><br/>
    	password:<input type="text" name="password"/><br/>
    	<input type="submit" value="ok"/>
    </form>
    ${userBean.name}
     <form action="<%=basePath%>login.action?method=validate3" method="post">
    	username:<input type="text" name="name"/><br/>
    	password:<input type="text" name="password"/><br/>
    	<input type="submit" value="ok"/>
    </form>
  </body>
</html>

 web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<servlet>
		<servlet-name>dipatcher</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>
	</servlet>

	<servlet-mapping>
		<servlet-name>dipatcher</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>
 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics