java后台开发例子--使用Maven建立springmvc的web项目

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yhhyhhyhhyhh/article/details/77102228

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下生成的项目文件。

猜你喜欢

转载自blog.csdn.net/yhhyhhyhhyhh/article/details/77102228
今日推荐