springMVC框架开发笔记 lesson1 springMVC编程

来源:互联网 发布:免费的翻墙软件 编辑:程序博客网 时间:2024/05/09 03:24

 SpringMVC

   WEB  MVC框架
   Spring 单独模块

   SpringMVC和Struts2 都是web mvc框架.

SpringMVC的常用的对象概念:

   前端核心控制器(DispatcherServlet)

      接受页面请求 调度  协调 页面的跳转等  

   Handler对象(相当于action对象): 处理器对象  

      调用模型(Service Dao等) 处理请求

   UrlMapping  映射器对象   

 负责将 url    Handler对象  进行映射对应

   适配器(Adapter):调用Handler

      调用对应 符合适配器规范  的Handler对象

  视图解析器: 

      前缀 /WEB-INF/jsp

      后缀  .jsp  

执行基本流程: Adapter(适配器对象)

|

  发出请求(login)-----前端核心控制器------映射器对象-------Hanlder处理器对象(LoginHandler) -----------视图解析器(视图名称上 可以加前缀和后缀)------>视图jsp      

SpringMVC编程:

Handler

public class HelloHandler implements Controller {

        //省略了属性的set方法

private  String hello;

private  String world; 

private  String viewJsp; 

public ModelAndView handleRequest(HttpServletRequest request,

HttpServletResponse response) throws Exception {

//Model: 模型 Map结构  存放业务模型数据   jsp页面上获取
//View:   视图 

//View:   视图 

ModelAndView mav = new ModelAndView();

mav.addObject("h", hello);//传值

mav.addObject("w", world);

mav.setViewName(viewJsp);//调转到页面

return mav;

}

}

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<bean id="helloHandler" name="/hello1.action" class="com.controller.HelloHandler">
<property name="hello">
<value>HelloWorld</value>
</property>
<property name="world">
<value>世界你好!!!</value>
</property>
<property name="viewJsp">
<value>hello</value>
</property>
</bean>
<!-- 配置请求url 和Handler映射器 -->
<!-- 第一种映射器 -->
<bean  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="hello.action">helloHandler</prop><!--跳转请求的url和bean id-->
--></props>
</property>
</bean>
<!--SimpleControllerHandlerAdapter
 只能调用Controller接口的Handler对象
   第一种适配器
 -->
<bean  class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
</bean>
<!-- 
 HttpRequestHandlerAdapter:
    只能调用实现HttpRequestHandler接口 Handler对象,
       第2种适配器
-->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter">
</bean>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

web.xml

   <!-- 配置springmvc的前端控制器 -->

  <servlet>

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

  <url-pattern>*.action</url-pattern>

  </servlet-mapping>


0 0