Spring+SpringMVC 分开配置
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_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloService.java
package org.javaboy.springmvc01.service;
import org.springframework.stereotype.Service;
/**
* @author 邓雪松 (づ ̄ 3 ̄)づ)
* @create 2021-10-30-18-56
*/
@Service
public class HelloService {
public String hello(String name){
return "hello "+name;
}
}
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">
<!--除了Controller别的都扫-->
<context:component-scan base-package="org.javaboy.springmvc01" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
MyController.java
package org.javaboy.springmvc01.controller;
import org.javaboy.springmvc01.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 邓雪松 (づ ̄ 3 ̄)づ)
* @create 2021-10-29-22-17
*/
@org.springframework.stereotype.Controller("/hello")
public class MyController implements Controller {
//将helloService注入到MyController
@Autowired
HelloService helloService;
/**
* 这个方法用来处理请求
* @param request 这个就是前端发送过来得请求
* @param response 这个就是服务端给前端得响应
* @return 返回 ModelAndView,Model 就相当于我们的 数据模型,View 则是视图
* @throws Exception
*/
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView("hello");//配置的试图名字就叫hello
mv.addObject("name","javaboy");//key:name value:javaboy
//打个HelloService的日志
System.out.println("helloService.hello(\"javaboy\")="+helloService.hello("javaboy"));
return mv;
}
}
spring-servlet.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">
<!--首先配置一个MyController的实例-->
<!--<bean class="org.javaboy.springmvc01.controller.MyController" name="/hello"/>-->
<!--下边是只扫controller,别的都不扫-->
<context:component-scan base-package="org.javaboy.springmvc01" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置处理器映射器,将前端请求的接口关联起来-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" id="handlerMapping">
<property name="beanName" value="/hello"/>
</bean>
<!--处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" id="handlerAdapter"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
浏览器访问:http://localhost:8081/hello
显示:hello javaboy!