【SpringMVC】Helloworld

来源:互联网 发布:fluke网络测试仪报价 编辑:程序博客网 时间:2024/05/22 03:02

SpringMVC就不同于Spring是处理java和xml的数据传输了,SpringMVC是处理views和controllers之间,网页和actions之间,说白了也就是java文件和jsp之间传输的东西。就是Servlet和Struts2那套,具体可以参考《【Servlet】最简单的Servlet JavaWeb程序》(点击打开链接)和《【Struts2】Struts2纯手工安装、配置以及Helloworld,以最新版struts 2.3.20 GA做例子》(点击打开链接),由于Servlet太原始显得太low,近年来Struts2也频频给没事找事的黑客爆出各种随便爆Struts2工程的漏洞,所以业内貌似有种都使用SpringMVC的趋势。SpringMVC和Spring是没啥绝对关系的,大家不要看Spring就觉得SpringMVC也是和Spring难得要死,其实这玩意入门难度也就和struts2差不多。下面举一个例子可以说明。


一、SpringMVC的下载和配置

其实就是《 【Spring】Spring3.x的下载与配置》(点击打开链接)需要的包,提取同样的包到你的JavaEE工程根目录下的WEB-INF\ib,最终使用的具体如下图所示,和Spring3.x需要的包是一模一样的,因为Spring希望他能够支配整个java工程,从javase到javaee再到android,反正有一个java一个xml的地方就有它。



二、工程目的和目录结构

下面的例子希望将HelloController.java设置的message打印到hello.jsp上面来。


工程的目录结构如下:



三、制作过程

1、首先是所有Javaee工程,WEB-INF下的核心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"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><servlet><servlet-name>SpringMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet><servlet-mapping><servlet-name>SpringMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>

核心配置文件,加载SpringMVC的同时指明这个加载SpringMVC的servlet的配置文件是XX-servlet.xml,具体如下。SpringMVC规定,所有Servlet配置文件都必须xx-serlvet.xml这样格式命名,如此web.xml配置完毕,SpringMVC就会自动为servlet找以其名字命名的xx-serlvet.xml进一步读取配置。



2、之后是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:p="http://www.springframework.org/schema/p"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-2.5.xsd      http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-2.5.xsd      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><context:component-scan base-package="test.actions" /><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/views/" /><property name="suffix" value=".jsp" /></bean></beans>  

这个文件完全指明了.java文件和.jsp之间个关系,具体如下图所示:


3、接下来马上来看test.action下的HelloController.java,配合上面的SpringMVC-servlet.xml和注释,相信大家已经非常清晰了,无须多加赘述了。

package test.actions;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloController {@RequestMapping(value = "/hello")//指明我这个方法是相应/hello这个链接public String hello(ModelMap model) {//这个方法名倒不是很重要,貌似没什么卵用,习惯上和@RequestMapping指明的相应连接匹配model.addAttribute("message", "你好,Spring MVC!");//指明hello.jsp的${message}的值是"你好,Spring MVC!"return "hello";//指明我相应完/hello会去到hello.jsp,将我的东西打印出来,真正做到MVC中的VC分离,习惯性相应的链接和.jsp匹配}/* 接下来要扩充,比如要加/a这个跳转,可以这样扩充: * 再下面接着开新方法@RequestMapping(value = "/a")public String a(ModelMap model) {//a需要的代码return "a";}*/}

4、最后是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>hello.jsp</title></head><body>从HelloController.java传过来的message=${message}</body></html>

大家可以看到,这里的SpringMVC在jsp打印变量的方法和《【Servlet】利用Servlet3.0标准与JSTL表达式实现文件上传系统,支持图片上传后显示》(点击打开链接)中的jstl表达式部分是一模一样,但做SpringMVC就不用自己再配一个jstl.jar和standard.jar,非常方便,应该该部分的内容已经在Spring的jar包中已经存在。

上面就是SpringMVC的一套基本流程。

原创粉丝点击