springMvc的简单使用:要求在浏览器发起请求,由springMVC接受请求并响应,将个人简历信息展示到浏览器

以下是一个简单的 Spring MVC 示例,用于在浏览器中发起请求并展示个人简历信息。

1. 项目结构

复制代码

|---------------------------------|
| src |
| └── main |
| ├── java |
| │ └── com |
| │ └── example |
| │ ├── controller |
| │ │ └── ResumeController.java |
| │ └── model |
| │ └── Resume.java |
| └── webapp |
| └── WEB-INF |
| └── views |
| └── resume.jsp |

2. 模型类 Resume.java

复制代码

java

|---------------------------------------------------------------------------------------------|
| package com.example.model; |
| |
| public class Resume { |
| private String name; |
| private String email; |
| private String phone; |
| private String education; |
| private String skills; |
| |
| // 构造函数、getters 和 setters |
| public Resume() {} |
| |
| public Resume(String name, String email, String phone, String education, String skills) { |
| this.name = name; |
| this.email = email; |
| this.phone = phone; |
| this.education = education; |
| this.skills = skills; |
| } |
| |
| // getters and setters |
| public String getName() { return name; } |
| public void setName(String name) { this.name = name; } |
| // 其他getter和setter... |
| } |

3. 控制器类 ResumeController.java

复制代码

java

|--------------------------------------------------------------|
| package com.example.controller; |
| |
| import com.example.model.Resume; |
| import org.springframework.stereotype.Controller; |
| import org.springframework.ui.Model; |
| import org.springframework.web.bind.annotation.GetMapping; |
| |
| @Controller |
| public class ResumeController { |
| |
| @GetMapping("/resume") |
| public String showResume(Model model) { |
| Resume resume = new Resume( |
| "张三", |
| "zhangsan@example.com", |
| "123-456-7890", |
| "计算机科学硕士", |
| "Java, Spring, MySQL" |
| ); |
| model.addAttribute("resume", resume); |
| return "resume"; |
| } |
| } |

4. 视图文件 resume.jsp

复制代码

jsp

|---------------------------------------------------------------------|
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| <html> |
| <head> |
| <title>个人简历</title> |
| </head> |
| <body> |
| <h1>个人简历</h1> |
| <p>姓名: ${resume.name}</p> |
| <p>邮箱: ${resume.email}</p> |
| <p>电话: ${resume.phone}</p> |
| <p>教育背景: ${resume.education}</p> |
| <p>技能: ${resume.skills}</p> |
| </body> |
| </html> |

5. Spring MVC 配置

确保你的 web.xml 和 Spring 配置文件(如 mvc-dispatcher-servlet.xml)已正确配置。

web.xml
复制代码

xml

|------------------------------------------------------------------------------------|
| <web-app> |
| <servlet> |
| <servlet-name>mvc-dispatcher</servlet-name> |
| <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> |
| <init-param> |
| <param-name>contextConfigLocation</param-name> |
| <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> |
| </init-param> |
| <load-on-startup>1</load-on-startup> |
| </servlet> |
| |
| <servlet-mapping> |
| <servlet-name>mvc-dispatcher</servlet-name> |
| <url-pattern>/</url-pattern> |
| </servlet-mapping> |
| </web-app> |

mvc-dispatcher-servlet.xml
复制代码

xml

|------------------------------------------------------------------------------------|
| <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/mvc |
| http://www.springframework.org/schema/mvc/spring-mvc.xsd"> |
| |
| <context:component-scan base-package="com.example.controller"/> |
| <mvc:annotation-driven/> |
| |
| <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> |
| <property name="prefix" value="/WEB-INF/views/"/> |
| <property name="suffix" value=".jsp"/> |
| </bean> |
| </beans> |

6. 运行和测试

  1. 将项目部署到 Tomcat 或其他 Servlet 容器。
  2. 访问 http://localhost:8080/your-app-context/resume

7. 运行结果截图

  • 浏览器中会显示如下内容:

    复制代码

    |----------------------------|
    | 个人简历 |
    | 姓名: 张三 |
    | 邮箱: zhangsan@example.com |
    | 电话: 123-456-7890 |
    | 教育背景: 计算机科学硕士 |
    | 技能: Java, Spring, MySQL |

注意事项

  1. 确保你的开发环境已配置好 Spring MVC 和 JSP 支持。
  2. 如果使用 Maven 或 Gradle,确保相关依赖已正确引入。
  3. 视图解析器配置的路径和后缀需要与你的 JSP 文件位置和名称匹配。

通过以上步骤,你可以实现一个简单的 Spring MVC 应用,用于展示个人简历信息。

相关推荐
想摆烂的不会研究的研究生1 小时前
每日八股——Redis(1)
数据库·经验分享·redis·后端·缓存
码熔burning1 小时前
MySQL 8.0 新特性爆笑盘点:从青铜到王者的骚操作都在这儿了!(万字详解,建议收藏)
数据库·mysql
猫头虎1 小时前
2025最新OpenEuler系统安装MySQL的详细教程
linux·服务器·数据库·sql·mysql·macos·openeuler
哈库纳玛塔塔2 小时前
放弃 MyBatis,拥抱新一代 Java 数据访问库
java·开发语言·数据库·mybatis·orm·dbvisitor
@LetsTGBot搜索引擎机器人3 小时前
2025 Telegram 最新免费社工库机器人(LetsTG可[特殊字符])搭建指南(含 Python 脚本)
数据库·搜索引擎·机器人·开源·全文检索·facebook·twitter
计算机毕设VX:Fegn08953 小时前
计算机毕业设计|基于springboot + vue动物园管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
冉冰学姐4 小时前
SSM校园排球联赛管理系统y513u(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·ssm 框架应用·开题报告、
Tony Bai4 小时前
【分布式系统】03 复制(上):“权威中心”的秩序 —— 主从架构、一致性与权衡
大数据·数据库·分布式·架构
wb043072015 小时前
SQL工坊不只是一个ORM框架
数据库·sql
至善迎风5 小时前
Redis完全指南:从诞生到实战
数据库·redis·缓存