一直用eclipse开发项目,今天突然看到有的人用idea,发现缺失是个好用的工具,但是新建spring项目的时候发现了一些问题,所以写下来,共同进步。
为实现两个一百年的奋斗目标、实现中华名族伟大复兴而努力
安装idea过程略,直接打开安装好的idea,直接上图,跟着博主操作吧,
选择file→new→project
选择maven→打上对号→单击下面资源→next
填写GroupId和ArtifactId后,点击next
可以根据自己的需要修改setting文件的位置与下载位置
选择项目保存路径,修改后点击finish按钮,创建项目
下载资源可能会很慢,建议按照如下博客修改下载资源地址
配置pom文件,添加资源
<?xml version="1.0" encoding="UTF-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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ceshi</groupId>
<artifactId>com.ceshi.project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>ceshi</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</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>
<spring.version>4.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>com.ceshi.project</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 http://maven.apache.org/ref/current/maven-core/default-bindings.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>
配置tomcat,操作如下图
填写名称,选择启动浏览器
选择启动war包,注意下面输入框写一个“/”就可以
选择下面两个项目,启动热部署,点击ok,项目配置完成,可以启动试一下
项目启动成功,此时默认进的页面是index.jsp页面
ok,继续配置spring 与 spring mvc,编写controller类,编写页面等操作
首先新建java文件夹与resource文件夹,如下图,需要配置一下文件夹的类型
点击按钮
选择modules→sources
选中需要更改的文件夹,操作顺序如下图,如果文件夹类型错了,可以在右侧修改,点击❌号即可删除之前的操作,修改后点击ok
新建controller类,右键java依次按照下图操作,填写包名,点击ok,
右键包名,右键class文件,填写类名后,点击ok
class内容如下图,共两个方法,一个用显示页面,一个用于返回一条数据
package com.ceshi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/ceshi")
public class CeshiController {
/**
* 测试页面
* @return
*/
@RequestMapping("/index")
public String ceshi(){
return "hello";
}
/**
* 测试返回数据
* @return
*/
@RequestMapping("/success")
@ResponseBody
public String index(){
return "success";
}
}
在web-inf文件夹下新建文件夹及jsp页面,路径如下图所示,hello.jsp对应上面测试页面中返回的页面(如果这样配置的话,还不能运行,下面讲,配置spring及视图解析器等)
右键resources新建xml文件,名称分别为ApplicationContext.xml,如下图,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context "
>
<!-- 启用注解 -->
<context:annotation-config/>
</beans>
新建ApplicationContext-mvc.xml文件,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<!-- 开启扫描controller包下面的注解 -->
<context:component-scan base-package="com.ceshi.controller">
</context:component-scan>
<!-- 配置SpringMVC的视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/views/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
新建404页面,如果访问出错,直接进404页面
web.xml增加配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" id="WebApp_1523244147927">
<display-name>ceshi</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 首先加载ApplicationContext,然后加载ApplicationContext-mvc里面的内容 -->
<context-param id="ParamValue_1523244374867">
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:ApplicationContext.xml
</param-value>
</context-param>
<!-- 配置核心控制器 -->
<servlet id="Servlet_1523244374585">
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 访问页面后缀 -->
<servlet-mapping id="ServletMapping_1523244374351">
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 出现错误访问页面 -->
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
</web-app>
至此,所有的配置已经完成,ok,重新启动项目,看看效果吧
看到返回数据及页面,说明我们的spring项目已经搭建成功了,ok,这次就写到这把,有啥问题可以在评论区交流
一个相信坚持写好代码可以改变生活的程序员小哥哥
坚持、热爱、奋斗、幸福