1. 程式人生 > >如何使用SpringMVC搭建Web專案——例子

如何使用SpringMVC搭建Web專案——例子

SpringMVC的web專案的一個簡單的例子

平時自己很少完整的搭建一個Java的Web程式,突然搭建一個web程式會不知所措,在這裡就記錄下自己搭建的一個簡單的例子。專案環境:eclipse,tomcat,maven,springmvc。

首先,新建一個maven專案springdemo,
1.專案springmvc的相關jar包

pom檔案如下:

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.xwh</groupId> <artifactId>springdemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version>
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId
>
org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.0.0</version> <configuration> <webResources> <resource> <directory>WebContent</directory> </resource> </webResources> </configuration> </plugin> </plugins> </build> </project>

2.配置spring上下文,這裡採用註解方式,在實際專案中也是使用的較多的,所以配置檔案箱單簡潔:

<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"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">
    <context:component-scan base-package="com.xwh.*"></context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>
  1. 配置web.xml 檔案,配置該檔案主要是讓servlet容器載入spring的上下文和配置spring的dispatchServlet。檔案也很簡單:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:web="http://java.sun.com/xml/ns/javaee" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web_1="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    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>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

到這裡所有的配置基本完成,接下來是編寫控制器了,
4.編寫控制器:

@Controller
@RequestMapping("/")
public class HomeController {
    @RequestMapping("home")
    public @ResponseBody String homePage(){
        return "This  is home page";
    }
    public String indexPage(){
        return "login";
    }

}

到這裡,簡單的web程式就算搭建好了,啟動tomcat,在瀏覽器中訪問http://localhost:8080/springdemo/home 可以看到返回了This is home page的頁面。

這裡搭建一個簡單的web應用,需要的配置還是比較多,需要配置spring的上下文和web.xml的servlet容器的配置,當然這些配置完全可以用註解的形式來完成,我個人比較喜歡用xml來配置。