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

springMVC_03注解完成hello案例

时间:2018-12-12 12:56:54      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:mode   com   res   prope   request   contex   div   property   pack   

 

 

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.运行结果

技术分享图片

 

springMVC_03注解完成hello案例

标签:mode   com   res   prope   request   contex   div   property   pack   

原文地址:https://www.cnblogs.com/aihuadung/p/10107648.html

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