码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMvc_Hello,SpringMVC!案例

时间:2017-08-21 19:39:45      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:jsp   imp   name   str   容器   instance   cep   let   web.xml   

step1.导包: spring-webmvc

step2.添加spring配置文件。

step3.配置DispatcherServlet。(web.xml)

  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  <!--
          初始化参数:用于指定spring配置文件的位置和文件名。
          注:
              DispatcherServlet的初始化方法会启动Spring容器,所以需要知道spring
              的配置文件的位置。
       -->
      <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>

step4.写Controller。

public class HelloController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
        System.out.println("handleRequest()");
        /*
         * ModelAndView有两个常用的构造器:
         * ModelAndView(String viewName)
         * ModelAndView(String viewName,Map data)
         * 注:
         *     viewName:视图名。
         * data:数据
         */
        return new ModelAndView("hello");
    }
    
}

step5.写jsp。

<h1>Hello,SpringMVC!</h1>

step6.配置HandlerMapping和ViewResolver。

 

<?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">
    
    <!-- 配置HandlerMapping -->
    <!-- 告诉DispatcherServlet(前端控制器)请求路径与Controller的对应关系 -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/hello.do">hc</prop>
            </props>
        </property>
    </bean>
    <!-- 配置Controller(处理器 )-->
    <bean id="hc" class="controller.HelloController"></bean>
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    
</beans>

 

SpringMvc_Hello,SpringMVC!案例

标签:jsp   imp   name   str   容器   instance   cep   let   web.xml   

原文地址:http://www.cnblogs.com/LWQ168/p/7406157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!