代永波

导航

 

保持简洁的流程,附加上一定说明,让新手一次搞定,是本文的目的。

1 创建maven web 项目

 

2 修改pop.xml 文件,引入spring mvc

打开pop.xml 配置spring:

      <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

用了一个小伎俩,因为版本老是要写,不如在dependencies的上面配置一个变量:

    <properties>
        <spring.version>4.0.0.RELEASE</spring.version>
    </properties>

存盘之后, maven 自动下载库到本地,然后建立引用,这是web.xml 里的黄色就不见了,完整的pop.xml 文件如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mr.test</groupId>
    <artifactId>test4</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>test4 Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>4.0.0.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>test4</finalName>
    </build>
</project>

3 修改web.xml 文件,配置spring mvc

打开web.xml:

  1. 修改 webapp
<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">

2) 添加 servlet

    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

url-pattern 为 / 表示,接管全部的请求,用 org.springframework.web.servlet.DispatcherServlet 类来处理

DispatcherServlet拦截匹配的请求,依据拦截规则分发到目标Controller来处理。

servlet的名字为hello,那么spring会按约定的找hello-servlet.xml 来注入它的bean

3) 配好完整 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>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

4 创建 servlet xml 文件.

在web.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">
    <context:component-scan base-package="com.mr.test.controller" />
    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

看上去一大坨,实际内容就3处:

     <context:component-scan base-package="com.mr.test.controller" />

表示要扫描包com.mr.test.controller下的全部class 注入进去,我们将在这个包下写我们自己的controller。

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

表示当Controller 中返回 index,实际要寻找的是/index.jsp 文件。

<mvc:annotation-driven />

表示可以使用注解 (好像没有这句也可以正常工作...)

 

5 创建controller 和 jsp 文件

 1) 在src/main/java里添加一个包:com.mr.test.controller

 与之前在hello-servlet.xml 文件中配置的对应。

 2) 添加一个Class 取名为:HelloControl,然后再里面添加一个函数

    public String hello() {
        return "hello";
    }

 3) 在类定义添加@Controller 表明是一个Controller ,会被spring 识别

 4) 在函数上添加注解@RequestMapping,和url 请求绑定起来,当请求这个url时,就执行此函数

 5) Controller的完整的代码:

@Controller
public class HelloController {
 
    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }
}

 

当用户请求/hello时,将执行此函数,返回hello,然后spring 就会找 /hello.jsp文件(在hello-servlet中有配置寻找的前缀和后缀)

然后将hello.jsp 文件返回给用户。

 

6) 建立hello.jsp文件

我们在web.xml 目录的同级,建立一个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; UTF-8">
<title>Hello</title>
</head>
<body>
hello world!
</body>
</html>

 这个 jsp 就是相当于View了,我们hello函数就相当于 Controller, Model还没有介绍,我们先运行跑起来再修改。

7) eclipse报错

在eclipse里,你会发现它提示里jsp的第一行有错误,仔细一看:

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

好吧,打开pop.xml 再配置一个 servlet进去:

       <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

 

6 测试运行

执行run as server ,选定 tomcat 7 来运行,启动后,访问:

http://localhost:8080/test4/hello

 

运行成功

7 添加参数

现在修改下要带入参数:

http://localhost:8080/test4/hello?username=david

1) 修改Controller,将函数hello改为如下样子:

    @RequestMapping("/hello")
    public String hello(String username, Model model) {
        model.addAttribute("username", username);
        return "hello";
    }

 

2) 接收参数并显示在jsp文件上,修改jsp :

将hello world!改成:

hello ${username} !

3) 再启动运行

model.addAttribute("username", username);

这句话就演示了如何使用model了,至此 mvc 就全了。开了一个好头,接下来好好琢磨吧。

 

附:使用jetty启动

如果改用jetty,而不用tomcat呢?

 

1) 打开pop.xml 文件,在build 子节点中添加:

       <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.1.0.v20131115</version>
            </plugin>
        </plugins>

 

2) 运行时,输入maven指令 jetty:run 。

 

3) 测试:

 

访问:http://localhost:8080/test4/hello?username=david

没有成功!

 

访问: http://localhost:8080/hello?username=david

成功!

 

相同的web.xml 文件,不同的服务器运行,结果还不一样!(嘿嘿)

posted on 2014-10-04 00:53  代永波  阅读(210)  评论(0编辑  收藏  举报