springmvc ---->helloworld

 

第一个HELLOWORLD

1:创建项目,导入jar包。

2:写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">

<!-- 配置 DispatcherServlet -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 --> 
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

不想写

  <init-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>classpath:springmvc.xml</param-value>

  </init-param>

    <!-- 
            实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的.
            默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml
springDispatcherServlet-servlet.xml
-->

3:然后写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="springmvc.hlw"></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>

4:写HelloWorld类

package springmvc.hlw;

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

@Controller
public class HelloWorld {
    /*
     * 1.@RequestMapping注解来映射请求的URL
     * 2.返回值通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver解析器,
     * 解析:prefix前缀+returnVal+后缀
     * 
     */
    @RequestMapping("/helloworld")
    public String hello(){
        System.out.println("HelloWorld");
        return "success";
    }
}

return "success"经过解析,跳转到/WEB-INF/views/success.jsp  return 后面是可以随便写的,但是要有对应的jsp文件。

posted @ 2016-10-19 21:57  陆伟  阅读(150)  评论(0编辑  收藏  举报