Spring mvc 入门1—hello world
构建一个spring mvc的web程序
开发环境
- Eclipse for J2ee(eclipse官网有下载)
- spring-framework-3.*.RELEASE(Spring官网有下载)
- tomcat 7
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <servlet> <servlet-name>hello</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
里面配置了一个servlet,名称为hello,拦截方法,拦截的Url是所有的url。
- 同样是在WEB-INF文件夹下面,建立一个以servlet-name名称开头的xml,例如本例子,要建立一个hello-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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <mvc:annotation-driven/> <context:component-scan base-package="com.baifan.controller"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
- 第一个配置表明使用注解方式来配置spring mvc
- 第二个配置声明注解类或者方法package的路径
- 第三个配置我下面再讲是什么作用。
- 下面就可以进行正式的开发了,做一个最简单的helloworld。新建一个类:
package com.baifan.controller; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping(value={"/","/hello"}) public String hello(Map<String, Object> map) { map.put("hello", "world"); System.out.println("hello"); return "hello"; } }
- @Controller表明这个类是一个控制器。可以和页面交互。
- 每个方法之前都加上@RequestMapping(value=”xxx”),表明当访问项目名/xxx时就可以进入当前方法,比如本项目,项目名:spring_mvc_01。要访问当前方法,可以使用localhost:8080/spring_mvc_01/或者localhost:8080/spring_mvc_01/hello进行访问。
- spring mvc方便的地方是这个map,暂时只讲map,当数据放入到这个map中的时候,在页面对这个map进行访问,就可以取出值了。
- 最后的return “hello”,在上面hello-servlet.xml配置了一个地址的前缀(prefix)和后缀(suffix),返回hello,就会对应prefix+hello+suffix。本例子就会跳转到:/WEB-INF/jsp/hello.jsp
5,/WEB-INF/jsp/hello.jsp
- 使用EL表达式访问Contoller里面的map。
<%@ 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> hello ${hello }</body> </html>
运行结果:
本人初写基础博客,有哪里做的不好的,请指教,谢谢。




浙公网安备 33010602011771号