eclipse中使用maven創建springMVC項目


SpringMVC教程--eclipse中使用maven創建springMVC項目

一、在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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://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;

 
@Controller
public 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>
復制代碼
復制代碼
轉載:https://www.cnblogs.com/daxiang2008/p/9663317.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM