feat(spring): 迁移Spring项目结构并集成SpringMVC功能

- 将Student类从com.ssm.di包移动到com.ssm.aop.xml包
- 添加SpringMVC控制器FirstController和相关JSP页面
- 配置springmvc.xml中的组件扫描和视图解析器
- 更新项目模块配置添加Web facet和依赖库
- 创建grademanagement-SpringMVCProject war工件配置
- 修改Student类移除注解配置改用XML配置方式
- 在测试类中调整Bean获取和使用方式
main
CesarH 1 month ago
parent ffedbbfa25
commit aa0000d19b

@ -0,0 +1,14 @@
<component name="ArtifactManager">
<artifact type="exploded-war" name="grademanagement-SpringMVCProject:war exploded">
<output-path>$PROJECT_DIR$/out/artifacts/grademanagement_SpringMVCProject_war_exploded</output-path>
<root id="root">
<element id="directory" name="WEB-INF">
<element id="directory" name="classes">
<element id="module-output" name="grademanagement-SpringMVCProject" />
</element>
<element id="directory" name="lib" />
</element>
<element id="javaee-facet-resources" facet="grademanagement-SpringMVCProject/web/Web" />
</root>
</artifact>
</component>

@ -11,6 +11,16 @@
</webroots>
</configuration>
</facet>
<facet type="web" name="Web2">
<configuration>
<descriptors>
<deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web2/WEB-INF/web.xml" />
</descriptors>
<webroots>
<root url="file://$MODULE_DIR$/web2" relative="/" />
</webroots>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
@ -19,5 +29,9 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" scope="PROVIDED" name="Tomcat 9.0.89" level="application_server_libraries" />
<orderEntry type="library" exported="" scope="PROVIDED" name="Spring-5.3.20" level="project" />
<orderEntry type="library" name="lib" level="project" />
<orderEntry type="library" name="lib (2)" level="project" />
</component>
</module>

@ -1,5 +1,13 @@
package com.ssm.controller;
public class FirstController {
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class FirstController {
@RequestMapping("/hello")
//编写控制器方法
public String hello(){
return "showFirst";
}
}

@ -2,8 +2,14 @@
<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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
</beans>
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置组件扫描 -->
<context:component-scan base-package="com.ssm.controller"></context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/view/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

@ -0,0 +1,16 @@
<%--
Created by IntelliJ IDEA.
User: cesar
Date: 2026/4/22
Time: 17:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>grademanagement-SpringMVCProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

@ -0,0 +1,16 @@
<%--
Created by IntelliJ IDEA.
User: cesar
Date: 2026/4/22
Time: 16:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<a href="hello"></a>
</body>
</html>

@ -3,7 +3,7 @@
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">
<bean id="student" class="com.ssm.di.Student">
<bean id="student" class="com.ssm.aop.xml.Student">
<property name="studentId" value="2025001"/>
<property name="studentName" value="张三"/>
<property name="className" value="计科2班"/>

@ -3,7 +3,7 @@
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">
<bean id="student" class="com.ssm.di.Student">
<bean id="student" class="com.ssm.aop.xml.Student">
<property name="studentId" value="2025001"/>
<property name="studentName" value="张三"/>
<property name="className" value="计科2班"/>

@ -7,11 +7,12 @@ public class Test {
public static void main(String[] args) {
// 加载Spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("bean-aop-xml.xml");
Student student = (Student) ac.getBean("student");
// 获取Student的Bean实例
Student student = (Student) ac.getBean("student");
Student stu = (Student) ac.getBean("student");
Department department = (Department) ac.getBean("department");
student.printInfo();
// 调用打印方法AOP会自动切入前置通知
student.printInfo();
department.printInfo();

@ -1,38 +1,19 @@
package com.ssm.di.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* - SpringBean
* @Component: Spring IoCBean
*/
@Component("student") // 指定Bean的id为student也可以不写默认是类名首字母小写
public class Student {
// 学生核心属性
@Value("2025001") // 简单类型注入:直接赋值,也可以从配置文件读取
private Integer studentId; // 学生ID学号
@Value("张三")
private String studentName; // 学生姓名
@Value("计科2班")
private String className; // 所属班级
@Value("计算机科学与技术")
private String major; // 所学专业
@Value("20")
private Integer age; // 年龄
@Value("13800138000")
private String phone; // 联系电话
// 无参构造方法Spring IoC创建Bean的前提
public Student() {
}
// get/set方法Spring依赖注入赋值用也可以省略但建议保留
// Getter & Setter
public Integer getStudentId() {
return studentId;
}
@ -81,16 +62,13 @@ public class Student {
this.phone = phone;
}
// toString方法方便测试输出
@Override
public String toString() {
return "Student{" +
"studentId=" + studentId +
", studentName='" + studentName + '\'' +
", className='" + className + '\'' +
", major='" + major + '\'' +
", age=" + age +
", phone='" + phone + '\'' +
'}';
// 打印学生信息不用toString
public void printInfo() {
System.out.println("学号:" + studentId);
System.out.println("姓名:" + studentName);
System.out.println("班级:" + className);
System.out.println("专业:" + major);
System.out.println("年龄:" + age);
System.out.println("电话:" + phone);
}
}

@ -3,7 +3,7 @@
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">
<bean id="student" class="com.ssm.di.Student">
<bean id="student" class="com.ssm.aop.xml.Student">
<property name="studentId" value="2025001"/>
<property name="studentName" value="张三"/>
<property name="className" value="计科2班"/>

Loading…
Cancel
Save