文章目录
- IDEA中搭建SSM项目
- 步骤一
- 步骤二
- 步骤三
- 步骤四
- 步骤五
- 步骤六
- 步骤七
- 步骤八
- 步骤九
- 步骤十
- 步骤十一
- 步骤十二
- 步骤十三
IDEA中搭建SSM项目
在IDEA2020中搭建SSM项目,并用ajax测试前后端的数据交互是否正常,过程详细,测试截图,最终有源码提供下载。(转载请附上原文链接)
步骤:
步骤一
1、打开IDEA,创建新项目。
步骤二
2、选择Maven项目,勾选“从原型创建”,选择Maven webapp,然后next。
步骤三
3、输入项目名、groupid和项目位置(GroupID 是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构),之后点击next。
步骤四
4、由于Maven项目创建时会下载很多依赖包,有好多用不到,所以在此处需加入一个properties配置:archetypeCatalog——internal,OK之后点击finish。
步骤五
5、之后初始项目目录是下图这样:
步骤六
6、在main目录下创建两个文件夹:java和resources,分别设置为sources root和resources root,在Java目录下创建zh包。
步骤七
7、配置pom.xml文件,加入相关依赖:spring-context,spring-web,spring-webmvc,servlet-api和jsp-api,同时将编译版本调至最初选择的jdk版本。
<?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>zh</groupId>
<artifactId>Test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Test Maven Webapp</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.8</maven.compiler.source>
<maven.compiler.target>1.8</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-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>Test</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>
步骤八
8、在resources文件夹下创建springmvc.xml配置文件,在其中:开启注解扫描的包、配置视图解析器和文件后缀、开启注解SpringMVC框架注解的支持。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="zh"/>
<!-- 配置视图解析-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 文件的后缀名-->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 开启注解SpringMVC框架注解的支持-->
<mvc:annotation-driven/>
</beans>
步骤九
9、配置web.xml文件,配置前端控制器。
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载springmvc配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 一启动服务器就创建此对象-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!-- 过滤所有-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
步骤十
10、编写前端页面,通过ajax发送数据。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<h2>SSM测试小例子</h2>
<div id="d1" style="border: dashed;height: 100px;width: 300px">
传入文本:<input type="text" id="send_text">
<button id="btn1">发送</button>
</div>
<div id="d2" style="border: dashed;height: 100px;width: 300px">
收到文本:<input type="text" id="receive_text">
</div>
<script>
$("#btn1").click(function () {
$.ajax({
url: "testssm", //后台url
type: "post",
data: {"sendmessage": $("#send_text").val()},
success: function (data) {
$("#receive_text").val(data)
}
})
});
</script>
</body>
</html>
步骤十一
11、创建Controller类。编写实现方法。
package zh;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
/**
* Created with IntelliJ IDEA.
* User: ZH
* Author: ***
* Date: 2020/05/15 14:29
* Description:
*/
@Controller
public class TestController {
@RequestMapping(path = "testssm",method = {RequestMethod.POST},produces="text/html;charset=UTF-8")
@ResponseBody
public String testDemo(String sendmessage, HttpServletResponse response) {
System.out.println("收到"+sendmessage);
String s = "hello SSM";
return sendmessage+"后台添加:"+s;
}
}
步骤十二
12、配置tomcat服务器。
步骤十三
13、启动服务器。浏览器自动跳转到index界面。
测试一下发送数据到后台:
点击发送后,后台已经处理完数据并返回来啦,填充到第二个文本框中。
至此IDEA搭建SSM项目完成,其他功能以此类推。
(记录学习所用,如有错误,望指正)