項目結構預覽

核心配置文件:spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:p="https://www.springframework.org/schema/p"
xmlns:context="https://www.springframework.org/schema/context"
xmlns:aop="https://www.springframework.org/schema/aop"
xsi:schemaLocation="
https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-3.0.xsd
https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-3.0.xsd
https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 啟用 @AspectJ AOP 支持 -->
<aop:aspectj-autoproxy/>
<!-- 掃描 com.yxl 包及子包下所有帶註解的類 -->
<context:component-scan base-package="com.yxl.*" />
<!-- 配置返回值處理器:支持 String 直接返回、對象自動轉 JSON -->
<bean id="handleAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON 轉換器 -->
<ref bean="mappingStringHttpMessageConverter" /> <!-- String 轉換器 -->
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<bean id="mappingStringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter" />
<!-- 視圖解析器:JSP 前綴/後綴 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>
<!-- 文件上傳編碼 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" />
</beans>
Web 部署描述符:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns="https://java.sun.com/xml/ns/javaee"
xmlns:web="https://java.sun.com/xml/ns/javaee/web-app_2.5.xsd"
xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2.5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springmvc1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 編碼過濾器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param>
<init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param>
</filter>
<filter-mapping><filter-name>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
<!-- DispatcherServlet 核心控制器 -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern> <!-- 攔截所有請求 -->
</servlet-mapping>
</web-app>
控制器實現

@Controller
@RequestMapping("/demo")
public class StringController {
@RequestMapping("/hello")
@ResponseBody // 直接返回字符串,不經視圖解析
public String hello() {
return "Hello Spring MVC!";
}
@RequestMapping("/json")
@ResponseBody
public User jsonDemo() {
return new User("張三", 25); // 自動轉 JSON
}
}
關鍵點總結
- DispatcherServlet 是前端控制器,攔截
/ 所有請求
context:component-scan 開啟註解掃描,自動註冊 @Controller
AnnotationMethodHandlerAdapter + MessageConverter 實現 @ResponseBody 直接返回數據
InternalResourceViewResolver 解析 JSP 視圖路徑
- 編碼過濾器確保中文不亂碼
現代化升級建議
- Spring Boot 2.3+:
spring-boot-starter-web 零配置啟動
- 使用
@RestController 合併 @Controller + @ResponseBody
- 視圖層遷移 Thymeleaf/FreeMarker 或前後端分離 (Vue/React)
- 配置類替代 XML:
@Configuration + @EnableWebMvc