SpringMVC框架的搭建步骤-002

来源:互联网 发布:数控车u型槽的编程 编辑:程序博客网 时间:2024/04/29 02:37

SpringMVC搭建步骤

一、配置DispatcherServlet

在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" id="WebApp_ID" version="3.0">  <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>

二、创建一个xml配置文件

servlet名称-servlet.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:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"    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.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"></beans>

三、配置HandlerMapping

此步骤可以忽略,spring会给我们一个缺省的配置一个叫做BeanNameUrlHandlerMapping的HandlerMapping,当然这个handlerMapping并不常用,现在用的比较多的是DefaultAnnotationHandlerMapping,因为该HandlerMapping是基于注解的,用起来比较方便。
当然此处为了方便起见,我们就不配置了,即使用spring的缺省配置

四、配置Controler

上面第二步创建好的spring配置文件hello-servlet.xml中的beans标签中假如如下配置:

<bean name="/welcome.html" class="zttc.itat.controller.WelcomeController" />

五、在src目录下创建控制器WelcomeController,继承AbstractController类

package zttc.itat.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;public class WelcomeController extends AbstractController{    @Override    protected ModelAndView handleRequestInternal(HttpServletRequest arg0,            HttpServletResponse arg1) throws Exception {        // TODO Auto-generated method stub        System.out.println("welcome");        return new ModelAndView("Welcome!!");    }}

这一步相当于Sturt2里面的创建Action,里面的handleRequest就相当于action里面的execute方法;

六、配置ViewResolver

在spring配置文件中的beans标签中假如如下配置:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/jsp/"/>        <property name="suffix" value=".jsp"/>    </bean>

七、创建jsp视图界面(welcome.jsp)

在WEB-INF/jsp目录下创建welcome.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>        Welcome!!!    </body></html>

到此,springMVC的hellowrold程序就搭建完毕。

八、测试

在tomcat中部署好环境之后,在浏览器中输入如下地址:

http://localhost:8080/springmvc_hello/welcome.html

效果图

将看到如下所示页面,

到此,表示环境搭建成功!

九、下面看基于注解的情况:

1、在spring配置文件的beans标签中添加如下代码:

<!-- 基于注解 -->    <context:component-scan base-package="zttc.itat.controller"/>    <mvc:annotation-driven />

2、在src目录的zttc.itat.controller包下创建HelloController如下:

package zttc.itat.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * 通过controller注解注入的controller * @author John * */@Controllerpublic class HelloController {    //requestMapping表示用哪个url来对应    @RequestMapping({"/hello","/"})    public String hello(){        return "hello";    }}

3、创建hello视图

在WEB-INF下创建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>        hello!    </body></html>

4、在浏览器输入如下地址:

http://localhost:8080/springmvc_hello/hello

将会看到如下界面:
效果图

表示搭建成功!

十、项目结构如下:

项目结构图

0 0
原创粉丝点击