当前位置: 代码迷 >> VC/MFC >> SpringMVC之hello world全世界
  详细解决方案

SpringMVC之hello world全世界

热度:132   发布时间:2016-05-02 03:37:13.0
SpringMVC之hello world世界
SpringMVC之hello world世界
一、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>SpringMVCLesson</display-name>     <servlet>        <servlet-name>SpringMVCLesson</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>         <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:springservlet-config.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup><!-- load-on-startup必须放在最后 -->    </servlet>    <!-- Spring MVC配置文件结束 -->        <servlet-mapping>        <servlet-name>SpringMVCLesson</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    </web-app>

引用
DispatcherServlet主要就是拦截请求,然后调用对应的Controller和Action,解析和渲染指定的视图并返回响应,所以之后配置只需配置classpath:springservlet-config.xml文件即可,注意启动顺序

2、配置springservlet-config.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"  xmlns:util="http://www.springframework.org/schema/util"  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/util  http://www.springframework.org/schema/util/spring-util.xsd  http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context.xsd  http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc.xsd" >        <!-- 默认的注解映射的支持 -->      <mvc:annotation-driven/>          <!-- 如果当前请求为“/”时,则转发到“/helloworld/index” -->    <mvc:view-controller path="/" view-name="forward:/helloworld/index"/>     <!-- 静态资源映射 -->    <mvc:resources mapping="/js/**" location="/WEB-INF/js/" />    <mvc:resources mapping="/css/**" location="/WEB-INF/css/" />    <mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" />    <mvc:resources mapping="/plugins/**" location="/WEB-INF/plugins/" />    <mvc:resources mapping="images/**" location="/WEB-INF/images/" />    <!-- 当上面要访问的静态资源不包括在上面的配置中时,则根据此配置来访问 -->    <mvc:default-servlet-handler/>    <!-- 开启controller注解支持 -->    <!-- use-default-filters="false" 只扫描指定的注解 -->    <context:component-scan base-package="com.demo.web.controllers" use-default-filters="false">        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>    </context:component-scan>          <!-- 视图解析器 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">       <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>       <property name="contentType" value="text/html"/>               <property name="prefix" value="/WEB-INF/views/"/>       <property name="suffix" value=".jsp"/>    </bean>    </beans>

3、Controller类
package com.demo.web.controllers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping(value = "/helloworld")public class HelloWorldController {    @RequestMapping(value="/index", method = {RequestMethod.GET})    public ModelAndView index(){                ModelAndView modelAndView = new ModelAndView();          modelAndView.addObject("message", "Hello World!");          modelAndView.setViewName("index");          return modelAndView;    }    }

引用
@Controller 注解把该class指定为controller

controller [email protected] 注解的 value值指定该controller所映射的请求

[email protected] 注解指定该方法为一个action,value 值指定该action所映射的请求,method 中的RequestMethod.GET指定该action只接受get请求

method = {RequestMethod.GET, RequestMethod.POST})表示该action可以接受get或post请求,不过更简单的是不对method做配置则默认支持所有请求方式

ModelAndView 中的setViewName指定了该action所对应的视图名称,解析视图时会在springservlet-config.xml文件指定的视图文件夹中寻找对应的视图
  相关解决方案