spring初学

来源:互联网 发布:java计算器布局 编辑:程序博客网 时间:2024/06/05 04:07

建立项目前注意的地方。

1、包一般是分层的,所以项目的包结构是flat。

2、我用eclpse,新建的是一个动态的web project项目。


流程:

浏览器发送请求---->DispatcherServlet(总的控制中心)---->分发器---->数据模型---->view。

对应

发送请求---->web---->spring---->数据库操作---->页面。

那问题就来了,他们是怎么做交接的?

问题一

1、浏览器发送请求后,Tomcat它怎么知道的呢?

Tomcat就是通过ServerSocket监听Socket的方式来接收客户端请求的。可以查看

2、web.xml与spring.xml怎么连接?

DispatcherServlet默认加载在WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml的文件。

3、spring连接数据库?

SqlSessionFactoryBean通过数据源和mapping的映射文件的整合,就可以连接数据库了。

4、spring 怎么返回一个页面?

spring是有一个ViewResolver(视图解析器)的组件。


web配置文件

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>springTest</display-name>    <!-- Spring MVC配置 --><!-- ====================================== --><servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value>  默认 </init-param> --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><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>  </web-app>

spring配置文件源码:

<?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:mvc="http://www.springframework.org/schema/mvc"          xmlns:context="http://www.springframework.org/schema/context"          xmlns:aop="http://www.springframework.org/schema/aop"          xmlns:tx="http://www.springframework.org/schema/tx"          xsi:schemaLocation="              http://www.springframework.org/schema/context               http://www.springframework.org/schema/context/spring-context-3.2.xsd              http://www.springframework.org/schema/beans               http://www.springframework.org/schema/beans/spring-beans-3.2.xsd              http://www.springframework.org/schema/mvc               http://www.springframework.org/schema/mvc/spring-mvc-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">  <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="mappings"><props><prop key="start.do">StartControler</prop></props></property></bean><bean id="StartControler" class="action.StartControler"></bean></beans>

StartControler源码:

public class StartControler implements Controller{@Overridepublic ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {System.out.println("spring");return null;}}

也就是会在控制台打印出spring。

下面贴下文章的项目目录和注意配置tomcat的配置。



最后,祝大家做个好梦。。。。。