7.26--SSH学习之SpringMVC小Demo

这里写图片描述

上图就是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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ssy03092919

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值