7.26--SSH学习之SpringMVC小Demo

来源:互联网 发布:centos 安装桌面 编辑:程序博客网 时间:2024/06/06 03:03

这里写图片描述

上图就是springMVC的运行的流程结构图:
路径7:创建处理器。
路径8、9、10:在传递过程中,处理器返回的是“ModelAndView”。
路径11:解析器把ModelAndView解析成JSP。


下面介绍一个简单的例子

功能:通过提交url,读取xml文件中的bean的name,来实现页面的跳转

创建控制器类常用的有两种方法:
1. 普通类实现Controller接口
2. 普通类实现HttpRequestHandler接口

由于创建的控制器类不同,因此处理器适配器也不同:

<!--  适用于实现Controller接口的处理器适配器 -->    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />    <!--  适用于实现HttpRequestHandler接口的处理器适配器 -->    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />


1. 提交表单:

<body>    <form action="trySecond.action" method="post" >        用户名:<input type="text" name="userName" value="su"><br>        密码:<input type="password" name="userPwd" value="1111"><br>        <input type="submit" value="登录">    </form>   </body>


2. 配置文件springMVC.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    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-4.3.xsd        http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-4.3.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd        http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop-4.3.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">    <!-- 配置处理器        / 代表从工程根目录开始访问-->    <bean id="firstContoller" name="/firstController.action" class="com.su.controller.FirstController">    </bean>    <bean id="secondContoller" name="/secondController.action" class="com.su.controller.SecondController">    </bean>    <!--  BeanName处理器映射器 将bean的name作为url进行查找 -->    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />    <!-- 简单url映射 -->    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">        <property name="mappings">            <props>            <!-- 可以多个key值(url)指向同一个Controller,多个Controller的配置都放在<props>标签中                 <prop>标签中间写的是ControllerBean的id -->                <prop key="/tryfirstOne.action">firstContoller</prop>                <prop key="/tryfirstTwo.action">firstContoller</prop>                <prop key="/trySecond.action">secondContoller</prop>            </props>        </property>    </bean>    <!--  适用于实现Controller接口的处理器适配器 -->    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />    <!--  适用于实现HttpRequestHandler接口的处理器适配器 -->    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />    <!--  视图解析器 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!--  配置jsp路径的前缀        <property name="prefix" value="/result/" />-->        <!--  配置jsp路径的后缀-->        <property name="suffix" value=".jsp" />    </bean> </beans>


3. 用第一种方法创建控制器类,FirstController.java

public class FirstController implements Controller {    @Override    public ModelAndView handleRequest(HttpServletRequest request,            HttpServletResponse response) throws Exception {        System.out.println("in FirstController method handleRequest()");        String userName = request.getParameter("userName");        String userPwd = request.getParameter("userPwd");        System.out.println("用户名:"+userName);        System.out.println("密码:"+userPwd);        ModelAndView mav = new ModelAndView();        //向ModelAndView对象中设置值,这个值保存在request中,重定向会使ModelAndView中值消失        mav.addObject("first", "cat");        //向session中设置值        request.getSession().setAttribute("first", "monkey");//      mav.setViewName("success.jsp");   //返回结果的页面,转发;"redirect:success.jsp",重定向;//      mav.setViewName("redirect:success.jsp");                mav.setViewName("success");     //在xml文件的视图解析器中,自动为success填充前缀或后缀        return mav;    }}


4. 跳转后的页面success.jsp页面:

 <body>    什么动物:${first }  </body>


5. 用第二种方法穿件控制器类SecondController.java

public class SecondController implements HttpRequestHandler {    @Override    public void handleRequest(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        System.out.println("in SecondController method handleRequest()");        String userName = request.getParameter("userName");        String userPwd = request.getParameter("userPwd");        System.out.println(userName);        System.out.println(userPwd);        request.setAttribute("first", "pig");        request.getSession().setAttribute("first", "dog");        //转发//      request.getRequestDispatcher("success.jsp").forward(request, response);        //重定向        response.sendRedirect("success.jsp");       }}



Author:su1573

原创粉丝点击