手把手教你用maven+springMVC创建一个工程

编程入门 行业动态 更新时间:2024-10-25 17:27:04

<a href=https://www.elefans.com/category/jswz/34/1765412.html style=手把手教你用maven+springMVC创建一个工程"/>

手把手教你用maven+springMVC创建一个工程

配置maven过程在此不在概述,直接到MVC配置,具体可参考我以前的这篇博客,

step1:先打开pom.xml,在里面插入如下代码,主要为了加入MVC依赖

<?xml version="1.0" encoding="UTF-8"?><project xmlns=".0.0" xmlns:xsi=""
xsi:schemaLocation=".0.0 .0.0.xsd">
<modelVersion>4.0.0</modelVersion><groupId>com.tutorial</groupId>
<artifactId>tutorial</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging><name>tutorial Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>;/url><properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties><dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
</dependencies><build>
<finalName>tutorial</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see .html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

step2:删除webAPP目录下的web.xml和index.jsp

step3:在main目录下新建一个命名为“Java”的文件,在该文件下新建一个命名为“ com.test.pluto.HelloController”的Java class,输入以下代码

package com.tutorial.pluto;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@RequestMapping("")
@Controller
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap modelMap){
modelMap.addAttribute("message","test");
return "hello";
}
}

输入之后,因为没有导入springframework.boot包的原因,会出现很多错误,点击File->setting->maven->勾选import maven automatically

step4:在web-inf下创建一个命名为“jsp”的文件,然后在其目录下面创建一个命名为“hello”的JSP,输入如下代码

<%--
Created by IntelliJ IDEA.
User: lbq
Date: 19-3-8
Time: 上午12:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
hello jsp
</body>

step5:在web-inf下创建一个命名“web.xml”,输入以下代码

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
".dtd" ><web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup></servlet><servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

step6:继续在web-inf下创建一个“ HelloWeb-servlet.xml”的文件,输入以下代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi=""
xmlns:context=""
xsi:schemaLocation="

.xsd

.xsd">
<context:component-scan base-package="com.tutorial.pluto"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

step7:配置Tomcat,单击run->edit configurations->单击左上方的+,找到Tomcat server,如果没有,点击 “33 items more”,,如果以前配置过得话,就会在,找到后,点击“local”, 选择Deployment,点击+,选择 Artifact…,选择 springMVC:war exploded

注意:Tomcat尽量下载安装版的,不要下载压缩包,对于新手来说,配置路径比较难,下载好后直接安装就行,在此之前必须提前配置好jdk的环境变量,不然会出现Tomcat界面闪退的情况,

step8:点击run即可

更多推荐

手把手教你用maven+springMVC创建一个工程

本文发布于:2023-07-28 17:49:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1267170.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:手把手   创建一个   教你用   工程   maven

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!