目录
参考:史上最详细的IDEA优雅整合Maven+SSM框架(详细思路+附带源码)_宜春-优快云博客_ssm框架idea整合代码
spring+struts2+ibatis和spring+springmvc+ibatis的区别是把struts2换成springmvc
在Idea 整合maven+ssi(spring+struts2+ibatis)_allensandy的博客-优快云博客的基础上进行改造,主要把struts2换成springmvc主要步骤有
1、pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
后面springmvc如果要配置文件上传要加入上传的jar包
2、web.xml
mvc框架部分把struts换成springmvc:
代码:
<!-- 2.springmvc的前端控制器 -->
<servlet>
<servlet-name>comqing-manager</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>comqing-manager</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3、spring/springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.steven.controller" />
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 视图解析 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 资源映射 -->
<mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>
<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
<!-- 定义文件上传解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设定默认编码 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
<property name="maxUploadSize" value="5242880"></property>
</bean>
</beans>
4、controller
package com.steven.controller;
import com.steven.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
/**
* @author steven
* @date 2021/10/20
*/
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/login")
@ResponseBody
public String login() {
String name = userService.getNameById(1);
return "WelCome login: " + name;
}
@RequestMapping(value = "/mdv")
public ModelAndView mdv() {
ModelAndView mav = new ModelAndView();
mav.setViewName("test");
mav.addObject("msg", "hello kaka!");
return mav;
}
}
5、其他代码
webapp/WEB-INF/jsp/test.jsp
<%--
Created by IntelliJ IDEA.
User: steven
Date: 2021/10/20
Time: 18:25
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>
收到的信息是${msg}
</body>
</html>
到此整合完毕