• springMVC_03注解完成hello案例


     

     

    1.导入jar包

      commons-logging-1.1.1.jar
      jackson-annotations-2.5.4.jar
      jackson-core-2.5.4.jar
      jackson-databind-2.5.4.jar
      spring-aop-4.1.6.RELEASE.jar
      spring-beans-4.1.6.RELEASE.jar
      spring-context-4.1.6.RELEASE.jar
      spring-core-4.1.6.RELEASE.jar
      spring-expression-4.1.6.RELEASE.jar
      spring-tx-4.1.6.RELEASE.jar
      spring-web-4.1.6.RELEASE.jar
      spring-webmvc-4.1.6.RELEASE.jar

    2.配置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>classpath:mvc.xml</param-value>
          </init-param>
          <!--设置这个servlet和服务器同步启动  -->
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>springmvc</servlet-name>
          <url-pattern>*.do</url-pattern>
      </servlet-mapping>

    3.创建配置文件mvc.xml在目录src下

    4.创建包com.ahd.controller,在包内创建类HelloController

    package com.ahd.controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class HelloController {
        @RequestMapping("hello")
        public ModelAndView hello(HttpServletRequest req,HttpServletResponse resp){
            ModelAndView mv=new ModelAndView();
            mv.addObject("msg", "controller annotat8ion");
            
            mv.setViewName("hello");
            
            return mv;
        }
    }

    5.修改配置文件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: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.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
            
        <!-- 配置渲染器 -->
        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <!-- 将视图名 渲染后视图的前缀 -->
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <!-- 渲染后视图的后缀 -->
            <property name="suffix" value=".jsp"/>
            <!-- 例:视图名为:hello   渲染后:/WEB-INF/jsp/hello.jsp 该页面-->
        </bean>
        <!-- 扫描指定包内的注解 -->
        <context:component-scan base-package="com.ahd.controller"></context:component-scan>
    </beans>

    6.在web-INF目录中建立文件夹jsp,在文件夹中建立文件hello.jsp,和视图渲染器配置相一致

    添加代码

    ${msg}

    7.运行结果

  • 相关阅读:
    java.lang.NoClassDefFoundError: org/jaxen/JaxenException解决方法
    解决Axis2在webservice中遇到特殊字符的无法传输的缺陷(<CDATA>数据类型)
    java使用POST发送soap报文请求webservice返回500错误解析
    HTTP直接请求webService
    Java占位符替换工具类
    sonarLint 插件配置sonarQube Server
    设计模式之十一:抽象工厂模式(Abstract Factory)
    oracle 序列重置
    windows快捷键之打开网络连接
    android中依据不同分辨率dp和px的相互转算
  • 原文地址:https://www.cnblogs.com/aihuadung/p/10107648.html
Copyright © 2020-2023  润新知