zoukankan      html  css  js  c++  java
  • springday03-go2

    新建springmvc01项目
    1.创建项目,导入jar包
    拷贝jar/spring/first下的五个spring的jar包,以及jar/spring/mvc下的两个mvcjar包放在lib下

    2.创建Spring MVC配置文件
    复制xml到src下,并将ApplicationContext.xml名字改为spring-mvc.xml

    3.在web.xml中配置DispatcherServlet
    在web.xml中配置DispatcherServlet,直接复制PPT上面的相关代码即可,注意,web.xml中通过DisPatcherservlet 的初始化方法启动了spring容器。之前我们在测试方法中总是要去写
    启动容器代码,现在不需要了。这里的代码只要写一次。

    4.创建Controller
    在controller包下新建HelloController类,要求实现Controller接口,返回的是一个ModelAndView

    5.配置Controller
    a.声明Controller b.定义请求处理映射HandlerMapping c.定义视图解析器ViewResolver

    6.创建页面
    在WEB-INF下新建一个文件hello.jsp

    8.部署-》启动Tomcat-》访问http://localhost:8088/springmvc/hello.do,则会输出JSP中的内容

    注意:很容易出现500错误,一定是xml配置文件路径有问题,注意检查路径!

    web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name></display-name>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    </web-app>

    HelloController.java:

    package controller;

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

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;

    //二级控制器:处理业务逻辑
    //要求实现Controller接口,只能处理一个请求。

    public class HelloController implements Controller{

    public ModelAndView handleRequest(
    HttpServletRequest req,HttpServletResponse res)
    throws Exception {
    System.out.println("helloController处理请求");
    //hello是视图名,ViewResolver会将视图名解析成真正的页面的名称,
    //比如解析成hello.jsp
    return new ModelAndView("hello");
    }


    }

     spring-mvc.xml:

    <?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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <bean id="helloController" class="controller.HelloController"/>


    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
    <props>
    <prop key="/hello.do">helloController</prop>
    </props>
    </property>
    </bean>

    <!-- 这样,视图名hello通过以下配置可以映射到/WEB-INF/hello.jsp -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/"/>
    <property name="suffix" value=".jsp"/>
    </bean>
    </beans>

    WebRoot/WEB-INF/hello.jsp:

    <h1>Hello,SpringMVC!</h1>

    结果:输入http://localhost:8088/springmvc01/hello.do:

    要求熟练掌握这个流程思想!

  • 相关阅读:
    Ibatis入门基本语法(转) good
    zip文件压缩(转)
    联合创始人股权分配,五五分是最糟糕的做法(转)
    家长如何检查孩子的家庭作业
    oracle存储过程实例
    MachineKey
    写写我那天參加过的《文明之光》书友会
    各种加解密算法比較
    算法分析---查找最大回文子串
    随机数是骗人的,.Net、Java、C为我作证
  • 原文地址:https://www.cnblogs.com/lchzls/p/5770422.html
Copyright © 2011-2022 走看看