1. 程式人生 > >java後臺開發例子--使用Maven建立springmvc的web專案

java後臺開發例子--使用Maven建立springmvc的web專案

java後臺開發例子–使用Maven建立springmvc的web專案

文章目錄


環境:

win7;jdk1.8_121;tomact8.0;

需要安裝及配置好jdk,mysql。

前言:

maven管理工程,非常方便,但是由於eclipse新建web專案的jdk和web版本比較低,在eclipse下利用maven建立springmvc的web專案,網上的教程都較為複雜,且配置經常出現請求不到或掃描不到control的情況,這裡記錄一下eclipse下利用maven建立springmvc的web專案最簡單的一個例子。

完整程式碼下載:

java後臺開發例子–使用Maven建立springmvc的web專案

1.測試

最終的例子的工程結構:

這裡寫圖片描述

測試效果:

這裡寫圖片描述

這裡寫圖片描述

maven install:

這裡寫圖片描述

2.詳細配置

2.1建立工程結構:

使用eclipse建立的maven web專案不符合正常的目錄結構,需要手動建立一些資料夾。

這裡寫圖片描述
這裡寫圖片描述

修改jdk版本,顯示maven新建web專案自帶的資料夾(這裡修改後,還需要在pom.xmL中配置,否則更新工程後,jdk版本又恢復預設的):

這裡寫圖片描述

專案右鍵建個source folder(測試用的,其實不建也不影響):

這裡寫圖片描述

刪除自動生成的index.jsp,在webapp\WEB-INF下建立views。

2.2新增jar包及配置jdk版本

複製下面pom.xml檔案內容或者在pom.xml中新增需要的spring,servlet,common-logging jar包。

注意:下面的pom.xml配置jdk版本為1.8了

<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>TestMaven</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>TestMaven Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
      <spring.version>4.3.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>
    
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
    <finalName>TestMaven</finalName>
  </build>
</project>

或者一個一個的新增在pom.xml中新增需要的spring,servlet,common-logging jar包。(手動新增jar包需要pom.xml配置jdk版本,否則update更新完後,需要在build path中設定jdk版本,麻煩
如圖新增依賴:
這裡寫圖片描述

2.3修改web版本:

修改前:
這裡寫圖片描述

修改後;

這裡寫圖片描述

如果不修改會出現紅叉且執行失敗:

這裡寫圖片描述

2.4建立專案

上述工程結構建立及配置完後,進入專案內容方面。

建立control類;建立spring配置檔案,修改web.xml,建立檢視jsp檔案等等

src\main\java下:ManygeSystem.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 ManygeSystem {
	@RequestMapping("/Login")  
	 public ModelAndView login(@RequestParam("username") String userName, 
			 @RequestParam("password") String passWord) {  
		    ModelAndView mv = new ModelAndView("menu");//指定檢視
	        mv.addObject("name", userName);
	        return mv;
	  }
}

spring DispatcherServlet配置檔案;

src\main\resources:springContext.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"/>
    <!-- 檢視頁面配置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix" value="/WEB-INF/views/" />  
        <property name="suffix" value=".jsp" />  
    </bean>
</beans>

web.xml(這裡是3.1版本):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>TestMaven</display-name>
    <welcome-file-list>
    <welcome-file>/WEB-INF/views/index.jsp</welcome-file>
  </welcome-file-list>
  <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>

views下的檢視檔案(測試)

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>登入</title>
</head>
<body>
<form action="Login" method="post">
<div>
賬號:<input type="text" name="username"/><br/>
密碼:<input type="text" name="password"/><br/>
<input type="submit" value="Login"/>
</div>
</form>
</body>
</html>

menu.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>java web開發mvc測試</title> 
</head> 
<body>
      <h1><font color="green">${name}  </font> 歡迎你!</h1>
</body> 
</html>

2.5更新,執行測試

上述檔案建立或修改後,都要儲存。

右鍵專案,maven ;update(一定要更新,所有的紅叉消失,如果不在2.3中修改web版本,還會有紅叉)。然後執行會得到1中的測試效果。

**注意:**常見執行失敗問題,請求不到control

(1)修改2.3中的web版本,與web.xml中的web版本一致。(一般都是直接從別的工程複製過來,然後修改)

(2)更新後maven install 失敗,未在pom.xml中配置jdk版本,這樣更新後maven web專案預設的jdk版本為1.5較低。

(3)一般修改後,執行前要更新一下工程。如果tomact啟動失敗需要重啟eclipse或者刪除當前server,重新建個server或者刪除tomact webapp下生成的專案檔案。