spring + maven 架構搭建

來源:互聯網
上載者:User

標籤:運算式   welcome   package   搭建   res   pen   his   搜尋   let   

本文轉自 http://www.cnblogs.com/qixing/p/qixing.html

一、在eclipse中建立maven-archetype-webapp項目:

  1.建立項目選擇maven項目

  

  2.預設,下一步

  

  3.選擇maven-archetype-webapp,其他保持預設即可

  

  4.如下填寫完成後,點擊完成即可

  

  5.建立完成後的maven項目結構如下

  

  其中index.jsp報錯,錯誤資訊:Multiple annotations found at this line: - The superclass "javax.servlet.http.HttpServlet" was not found on the Java

  意思是缺少servlet包,我們可以匯入javax.servlet-api-3.1.0.jar包,我們可以用兩種方式來處理:

    1> 在pom.xml中的dependencies中加入依賴包

    

1     <dependency>2         <groupId>javax.servlet</groupId>3         <artifactId>javax.servlet-api</artifactId>4         <version>3.1.0</version>5     </dependency>    

    2> 可以在build path中添加tomcat 庫,如下

    

    點擊next出現下面介面,如下操作

    

  至此,一個正常的maven web項目已經建好,如下:

  

二、配置springMVC

  1.在pom.xml中添加對spring的依賴

    pom.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.test</groupId>  <artifactId>HelloSpringMVC</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>HelloSpringMVC Maven Webapp</name>  <url>http://maven.apache.org</url>    <properties>      <spring.version>4.1.1.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>        <dependency>        <groupId>javax.servlet</groupId>        <artifactId>javax.servlet-api</artifactId>        <version>3.1.0</version>    </dependency>  </dependencies>  <build>    <finalName>HelloSpringMVC</finalName>  </build></project>

    儲存後會下載對應的jar檔案

  2.編輯web.xml檔案

    web.xml內容

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">      <display-name>Archetype Created Web Application</display-name>    <servlet>      <servlet-name>dispatcher</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>          <init-param>             <param-name>contextConfigLocation</param-name>          <param-value>classpath:springContext.xml</param-value>      </init-param>      <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>      <servlet-name>dispatcher</servlet-name>      <url-pattern>/</url-pattern>  </servlet-mapping>  <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:springContext.xml</param-value>  </context-param>  <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener></web-app>

 

  3.建立springContext.xml檔案,在src/main/resources包中建立springContext.xml檔案,

     

  springContxt.xml內容

  

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">        <!-- 搜尋spring控制項 -->    <context:component-scan base-package="com.test"></context:component-scan>    <!-- 視圖頁面配置 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix">            <value>/WEB-INF/views/</value>        </property>        <property name="suffix">            <value>.jsp</value>        </property>    </bean></beans>

   在springContext.xml中,base-package是指定spring控制器控制項的包,首碼指定的是視圖目錄,被設定為/WEB-INF/views,即視圖目錄被放到WEB-INF下。尾碼指定的是視圖的副檔名。例如,"hellospring"視圖,將被放到/WEB-INF/views/hellospring.jsp。

  

  4. 建立Spring控制器和視圖

  建立HelloSpringController.java類,在src/main/java包中,如:

  

  HelloSpringController.java

package com.test;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.servlet.ModelAndView; @Controllerpublic class HelloSpringController {    String message = "Welcome to Spring MVC!";     @RequestMapping("/hello")    public ModelAndView showMessage(@RequestParam(value = "name", required = false, defaultValue = "Spring") String name) {         ModelAndView mv = new ModelAndView("hellospring");//指定視圖
     //向視圖中添加所要展示或使用的內容,將在頁面中使用 mv.addObject("message", message); mv.addObject("name", name); return mv; }}

  在上面的代碼中,@Controller註解為Spring標註前置控制器的方式,@RequestMapping註解映射web請求到具體要操作的類或者方法上面,@RequestMapping註解既可以用到類上,也可以用到方法上,在此不再詳述,如有疑問,可以百度。@RequestParam註解為請求指定參數。這種方式提供了一個一致 的編程風格。

  另外上述代碼中ModelAndView類指定具體的視圖,這裡是"hellospring",由於我們在springContext.xml配置了視圖的前尾碼,所以在這裡只需要寫出視圖的具體名稱即可,其具體指定的就是:首碼+視圖名稱+尾碼,即完整的視圖路徑/WEB-INF/views/hellospring.jsp,也就是所要展示的視圖的位置。

  項目首頁index.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>Spring 4 MVC - HelloWorld Index Page</title></head><body>     <center>        <h2>Hello World</h2>        <h3>            <a href="hello?name=zhangsan">點擊跳轉</a>        </h3>    </center></body></html>

  上述代碼中,點擊跳轉 的連結其實就是我們HelloSpringController.java中定義的控制器的一個@RequestMapping註解方法,name=zhangsan為showMessage接受的參數。

  在/WEB-INF/views/下建立hellospring.jsp視圖,

  

  hellospring.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>Spring 4 MVC -HelloWorld</title></head><body>    <center>        <h2>Hello World</h2>        <h2>            ${message} ${name}     </h2>    </center></body></html>

  上述代碼中顯示我們在HelloSpringController.java的showMessage方法中添加的兩個參數message和name,

 

 

  好,至此我們使用maven搭建springMVC的操作已經全部完成,完成後整個項目的結構如:

  

三、將項目部署到tomcat伺服器運行

  首頁 http://localhost:8080/HelloSpringMVC/  ,其中也可以使用 http://localhost:8080/HelloSpringMVC/index.jsp 兩個效果是一樣的

  

  點擊跳轉 頁 

  

 

注意:若是跳轉後頁面直接顯示${message} ${name}說明jstl運算式不起作用,我們可以在pom.xml中添加如下依賴:

     <dependency>             <groupId>javax.servlet</groupId>              <artifactId>jstl</artifactId>              <version>1.2</version>              <scope>runtime</scope>          </dependency>         <dependency>            <groupId>taglibs</groupId>            <artifactId>standard</artifactId>            <version>1.1.2</version>        </dependency>

 

spring + maven 架構搭建

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.