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

SpringMvc-helloword

时间:2017-06-07 10:05:44      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:sys   ann   utf-8   out   转发   type   如何   efi   code   

1.springmvc的helloWord

1.建立hellowordweb项目

2.在web.xml中配置前端的请求路劲,把请求接入到springMvc中。

 1     <servlet>
 2     
 3         <servlet-name>dispatcherServlet</servlet-name>
 4     
 5         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 6     
 7         <!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 -->
 8         <!-- 
 9             实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的.
10             默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml
11         -->
12         <!--  
13         <init-param>
14             <param-name>contextConfigLocation</param-name>
15             <param-value>classpath:springmvc.xml</param-value>
16         </init-param>
17         -->
18         <load-on-startup>1</load-on-startup>
19 
20     </servlet>

2.配置springMvc.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:mvc="http://www.springframework.org/schema/mvc"
    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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 配置自定扫描的包 -->
    <context:component-scan base-package="com.atguigu.springmvc"></context:component-scan>

    <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

3.配置请求处理器(controller

package com.atguigu.springmvc.handlers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorld {

    /**
     * 1. 使用 @RequestMapping 注解来映射请求的 URL
     * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于 InternalResourceViewResolver 视图解析器, 会做如下的解析:
     * 通过 prefix + returnVal + 后缀 这样的方式得到实际的物理视图, 然会做转发操作
     * 
     * /WEB-INF/views/success.jsp
     * 
     * @return
     */
    @RequestMapping("/helloworld")
    public String hello(){
        System.out.println("hello world");
        return "success";
    }
    
}

 

SpringMvc-helloword

标签:sys   ann   utf-8   out   转发   type   如何   efi   code   

原文地址:http://www.cnblogs.com/csy666/p/6955080.html

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