Spring Web MVC

我是初学,了解的不深,若有错误望指出。也是为了以后方便自己的复习和监督自己学习。
Spring Web MVC 需要使用IOC功能
用于开发MVC结构的Web程序

1、MVC思想

将程序组件分为模型,视图,控制器三部分
流程: 发请求给控制器,控制器调用模型并响应,然后控制视图并响应 最后响应给请求的。

这里写图片描述

但是这个是一个控制器。如果比较大的话就不够了。

这里写图片描述

2、Spring如何实现MVC
3、Spring Web MVC处理流程
组件构成及流程

这里写图片描述

案例 hello.jsp
在WEB-INF下创建hello.jsp
不能直接访问到。
要做
/hello.do
DispatcherServlet(配置)
HanlderMapping(配置)
HelloController(要编写+配置)
ViewResolver
/WEB-INF/hello.jsp(编写)

a.搭建Spring Web MVC开发环境

引入 ioc,webmvc开发包。src中添加配置文件

b.编写HelloController

规则实现Contriller接口

package Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {

        ModelAndView mav = new ModelAndView();
        mav.setViewName("hello");//设置视图名 hello.jsp就写hello
        mav.getModel().put("msg", "模型数据");//设置要传出去的模型数据(传到jsp)
        //等价于 request.setAttrobute("msg","模型数据");
        //之后jsp就可以用el表达式来获取 ${msg}

        return null;
    }

}

在web.xml设置DispatcherServlet其路径

这里写图片描述

可以选中包名直接Ctrl+C复制,直接粘贴。

在 web.xml 写:

<servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 指明配置文件 -->
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpatch:applicationContext.xml</param-value>
  </init-param>
  </servlet>

  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>

c.配置 handlermapping 在Spring配置文件中写。

然后注意也要写路径同样像上面那样获取。

注意是这个下面的 org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.class
不是org.springframework.web.servlet.HandlerMapping.class
!!!

这里写图片描述

<!-- 配置handlermapping -->

    <bean id="handlermapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <!-- 指定请求和controller对应关系 -->
    <property name="mapping">
    <props>
    <!-- 几个请求就写几个prop -->
    <prop key="/hello.do">helloController</prop>
    </props>

    </property>
    </bean>

d.配置 Controller

<!-- 配置controller -->

    <bean id="helloController" class="Controller.HelloController">

    </bean>

e.配置 viewresolver

org.springframework.web.servlet.view.InternalResourceViewResolver
这里写图片描述

这里写图片描述

遇到了一些错误如下:

1、mappings写成了mapping …

Servlet.init() for servlet springmvc threw exception

<!-- 配置handlermapping -->

    <bean id="handlermapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <!-- 指定请求和controller对应关系 -->
    <property name="mappings">
    <props>
    <!-- 几个请求就写几个prop -->
    <prop key="/hello.do">helloController</prop>
    </props>

    </property>
    </bean>

2.控制器方法我没有将其返还
默认返回的空
就会出现网页一片空白什么也没有的状态

入门事例 登陆:

1、显示登陆页面

/login.do
DispatcherServlet
handlermapping
LoginController
viewResolver
/WEB-INF/login.jsp

因为配置的都配置完了,需要写一个Controller来实现控制进入哪个视图。

public class LoginController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        ModelAndView mav = new ModelAndView("login");
        //调用login.jsp
        // TODO Auto-generated method stub
        return mav;
    }

因为没有给login.jsp转发数据。所以直接创建 ModelAndView 的直接可以写视图名即 login

然后并在Spring配置文件中写

</bean>
    <!-- 写入LoginController -->
    <bean id="loginController" class="Controller.LogionController">

    </bean>

访问 login.do即进入
这里写图片描述

增加 实现点击登陆后进入登陆后的界面
/logon.do(提交表单)
handlermapping
Login2Controller

public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        // TODO Auto-generated method stub

        ModelAndView mav = new ModelAndView();

        String username = arg0.getParameter("username");
        String password = arg0.getParameter("password");

        if(username.equals("root")&&password.equals("1234")){
            mav.getModel().put("username", username);
            mav.getModel().put("password", password);
            mav.setViewName("hello");
        }
        else{
            mav.setViewName("logon");
        }



        return mav;

我把上面的登陆界面改成tologin.do 然后提交表单改为login.do

<prop key="/hello.do">helloController</prop>
    <prop key="/tologin.do">loginController</prop>
    <prop key="/login.do" >login2Controller</prop>

(若用户名是root,密码为1234则正确)

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pro Spring MVC provides in-depth coverage of Spring MVC and Spring Web Flow, two highly customizable and powerful web frameworks brought to you by the developers and community of the Spring Framework. Spring MVC is a modern web application framework built upon the Spring Framework, and Spring Web Flow is a project that complements Spring MVC for building reusable web controller modules that encapsulate rich page navigation rules. Along with detailed analysis of the code and functionality, plus the first published coverage of Spring Web Flow 2.x, this book includes numerous tips and tricks to help you get the most out of Spring MVC, Spring Web Flow, and web development in general. Spring MVC and Spring Web Flow have been upgraded in the new Spring Framework 3.1 and are engineered with important considerations for design patterns and expert object-oriented programming techniques. This book explains not only the design decisions of the frameworks, but also how you can apply similar designs and techniques to your own code. This book takes great care in covering every inch of Spring MVC and Spring Web Flow to give you the complete picture. Along with all the best known features of these frameworks, you'll discover some new hidden treasures. You'll also learn how to correctly and safely extend the frameworks to create customized solutions. This bookis for anyone who wishes to write robust, modern, and useful web applications with the Spring Framework. What you'll learn Key Spring Framework fundamentals How to use the Spring MVC architecture How to develop with the DispatcherServlet How to write Controllers How to work with Resolving and Implementing Views What are the supported View Types How to customize your website What isand how to use the Spring Web Flow framework How to test your Spring MVC applications How to implement Spring Security Who this book is for This book is for Spring or Java EE application developers who want to learn and fully leverage Spring's Web-tier creator and framework, Spring MVC. This book is also for experienced enterprise and Java programmers who are new to the Spring Framework. Table of Contents Configuring a Spring Development Environment Spring Framework Fundamentals Web Application Architecture Spring MVC Architecture Implementing Controllers Implementing Controllers Advanced REST and AJAX Resolving and Implementing Views Testing Spring MVC Applications Spring Web Flow Building Applications with Spring Web Flow Advanced Spring Web Flow Spring Security Cloud Foundry - Deploying to the Cloud http://item.jd.com/1135750085.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值