SpringMVC入门

f38327782 8年前

来自: http://my.oschina.net/u/2245781/blog/608216


一、建立web项目SpringMVC并导入spring包

WebContent/WEB-INF/web.xm

<?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_2_5.xsd"   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   id="WebApp_ID" version="2.5">   <display-name>spring mvc</display-name>   <welcome-file-list>    <welcome-file>index.jsp</welcome-file>   </welcome-file-list>     <!--configure the setting of springmvcDispatcherServlet and configure the     mapping -->   <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:springmvc-servlet.xml</param-value>    </init-param>    <!-- <load-on-startup>1</load-on-startup> -->   </servlet>     <servlet-mapping>    <servlet-name>springmvc</servlet-name>    <url-pattern>/</url-pattern>   </servlet-mapping>    </web-app>

src/springmvc-servlet.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.1.xsd          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                             <!-- scan the package and the sub package -->      <context:component-scan base-package="test.SpringMVC"/>         <!-- don't handle the static resource -->      <mvc:default-servlet-handler />         <!-- if you use annotation you must configure following setting -->      <mvc:annotation-driven />             <!-- configure the InternalResourceViewResolver -->      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"              id="internalResourceViewResolver">          <!-- 前缀 -->          <property name="prefix" value="/WEB-INF/jsp/" />          <!-- 后缀 -->          <property name="suffix" value=".jsp" />      </bean>  </beans>

二、控制器

 

package test.SpringMVC;    import org.springframework.stereotype.Controller;  import org.springframework.web.bind.annotation.RequestMapping;    @Controller  @RequestMapping("/mvc")  public class mvcController {    @RequestMapping       public String execute(){           return "default_page";       }      @RequestMapping("/hello")      public String hello(){                  return "hello";      }  }

@RequestMapping 有两种标注方式,一种是标注在类级别上,一种是标注在方法级别上。
标注在方法上时,value 表示访问该方法的 URL 地址。标注在类上时,value 相当于一个命名空间,即访问该 Controller 下的任一方法都需要带上这个命名空间。

(1)/mvc.action或/mvc执行的是 execute() 方法,这时根据springmvc-servlet.xml里配置,控制器将派发/WEB-INF/jsp/default_page.jsp页面。execute() 方法的 @RequestMapping 注解缺省 value 值,在这种情况下,当访问命名空间时默认执行的是这个方法。方法级别上的 @RequestMapping 标注是必须的,否则方法无法被正确访问。

(2)/mvc/hello.action或/mvc/hello.action执行的是hello() 方法,这时控制器将派发/WEB-INF/jsp/hello.jsp页面。类级别上的 @RequestMapping 标注不是必须的,在不写的情况下,方法上定义的 URL 都是绝对地址,否则,方法上定义的 URL 都是相对于它所在的 Controller 的。

三、jsp页面

WebContent/WEB-INF/jsp/default_page.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"      pageEncoding="UTF-8"%>  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  <html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  <title>Insert title here</title>  </head>  <body>             恭喜你!Spring MVC default page测试成功!  </body>  </html>

WebContent/WEB-INF/jsp/hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"      pageEncoding="UTF-8"%>  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  <html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  <title>Insert title here</title>  </head>  <body>             恭喜你!测试成功  </body>  </html>

WebContent/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"      pageEncoding="UTF-8"%>  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  <html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  <title>Insert title here</title>  </head>  <body>   <ul>    <li><a href="mvc/hello.action">Spring MVC HelloWorld!</a></li>    <li><a href="mvc">Spring MVC 默认页面 !</a></li>   </ul>  </body>  </

三、运行结果