SpringMVC学习使用一:搭建简单的SpringMVC开发项目环境并使用

来源:互联网 发布:开淘宝都买宝马了 编辑:程序博客网 时间:2024/05/30 19:34

Spring Web MVC的作用:

让我们能非常简单的设计出干净的Web层和薄薄的Web层;
√进行更简洁的Web层的开发;
√天生与Spring框架集成(如IoC容器、AOP等);
√提供强大的约定大于配置的契约式编程支持;
√能简单的进行Web层的单元测试;
√支持灵活的URL到页面控制器的映射;
√非常容易与其他视图技术集成,如Velocity、FreeMarker等等,因为模型数据不放在特定的API里,而是放在一个Model里(Map数据结构实现,因此很容易被其他框架使用);
√非常灵活的数据验证、格式化和数据绑定机制,能使用任何对象进行数据绑定,不必实现特定框架的API;
√提供一套强大的JSP标签库,简化JSP开发;
√支持灵活的本地化、主题等解析;
√更加简单的异常处理;
√对静态资源的支持;
√支持Restful风格。

一:首先导入springmvc所需要的jar包

二:配置web.xml文件,添加一个servlet拦截.do结尾的请求,也可以拦截所有的请求,其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_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>SpringMvc01</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>   <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:spring-mvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping></web-app>
三:springmvc也有专门的配置文件,spring-mvc.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"    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.xsd"><!-- 使用注解的包,包括子集 -->    <context:component-scan base-package="com.levi"/>    <!-- 视图解析器 --><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp"></property></bean></beans>

上面<property name="prefix" value="/WEB-INF/jsp/" />代表前缀
<property name="suffix" value=".jsp"></property>代表后缀

最后生成的页面应该是/WEB-INF/jsp/jsp页面名称.jsp

四:编写一个ctroller类

package com.levi.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloWorldController{@RequestMapping("/helloWorld")public String helloWorld(Model model){model.addAttribute("message","StringMVC,你好");return "helloWorld";}}
上面是直接用注解配置的,非常方便。其中那个RequestMapping里的内容就和struts2中的action是一样的。

五:最后新建一个helloWorld.jsp文件,其代码如下:

<span style="font-size:18px;"><%@ 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>${message }</body></html></span>


最后启动项目,在浏览器访问http://localhost:8080/SpringMvc01/helloWorld.do,最后就会显示:StringMVC,你好






0 0
原创粉丝点击