idea第一个SSM项目
- 第一个idea web xml-ssm项目
- maven项目,不使用模板。需要手动生成web.xml
pom.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--输入com,提示标签-->
<!--配置扫描-->
<!--配置基准扫描路径-->
<!--使用默认拦截器-->
<!--排除拦截器:排除Controller类-->
<!--不要选择这个类 expression="org.springframework.web.servlet.mvc.Controller"-->
<context:component-scan base-package="com.cjw" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
applicationContext.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--输入com,提示标签-->
<!--配置扫描-->
<!--配置基准扫描路径-->
<!--使用默认拦截器-->
<!--排除拦截器:排除Controller类-->
<!--不要选择这个类 expression="org.springframework.web.servlet.mvc.Controller"-->
<context:component-scan base-package="com.cjw" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
spring-service.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: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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--spring-mvc的配置文件-->
<!--只扫描Controller类-->
<!--包含拦截器:扫描Controller类-->
<context:component-scan base-package="com.cjw" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--???-->
<mvc:annotation-driven />
<!--???-->
<!--静态资源文件路径相关-->
<mvc:resources mapping="/**" location="/"/>
<!--<mvc:resources mapping="/css/*" location="/css/"/>-->
<!--<mvc:resources mapping="/img/*" location="/img/"/>-->
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--tomcat启动时就加载spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--深究原理-->
<!--ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置springmvc调度器-->
<!--加载springmvc的配置文件-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-service.xml</param-value>
</init-param>
</servlet>
<!--servlet映射路径-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
测试
目录结构
package com.cjw.controller;
import com.cjw.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public String sayHello() {
return helloService.hello();
}
}
package com.cjw.service;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
public String hello(){
return "hello";
}
}
项目打包
项目打包 提取码:n3n1
环境:idea,ssm