SpringMVC 入门

简介: SpringMVC 入门

3.创建请求控制器

由于前端控制器对浏览器发送的请求进行了统一的处理,但是具体的请求有不同的处理过程,因此需要创建处理具体请求的类,即请求控制器
请求控制器中每一个处理请求的方法成为控制器方法
因为SpringMVC的控制器由一个POJO(普通的Java类)担任,因此需要通过@Controller注解将其标识为一个控制层组件,交给Spring的loC容器管理,此时SpringMVC才能够识别控制器的存在

  • 编写视图层
@Controller
public class HelloController {

}
  • 开启组件扫描
<?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  http://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫描组件-->
    <context:component-scan base-package="cn.zhao.controller"></context:component-scan>
</beans>
  • 配置视图解析器
<!--配置 Thymeleaf视图解析器-->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    <property name="order" value="1"/>
    <property name="characterEncoding" value="UTF-8"/>
    <property name="templateEngine">
    <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
        <property name="templateResolver">
            <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                <!--视图前缀-->
                <property name="prefix" value="/WEB-INF/templates/"/>
                <!--视图后缀-->
                <property name="suffix" value=".html"/>
                <property name="templateMode" value="HTML5"/>
                <property name="char acterEncoding" value="UTF-8"/></bean>
        </property>
     </bean>
  </property>
</bean>

4.访问首页

  • 配置访问路径
@Controller
public class HelloController {

    /**
     * //" / "-->/wEB-INF/templates/index.html
     * @return
     */
    @RequestMapping(value = "/")
    public String index(){
        // 返回视图名称
        return "index";
    }
}
  • 添加pom.xml
<!--静态资源导出问题-->
<build>
    <plugins>
        <!-- 配置web.xml文件的配置 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <!-- 指定web.xml的路径  -->
                <webXml>webapp\WEB-INF\web.xml</webXml>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>
  • 测试

image.png

  • 如果访问不了,手动建立lib,导入jar包,才不会发现load加载异常
  • 或者给打包方法改为war包就行了
相关文章
|
Python
Python 3.10 版本采纳了首个 PEP,中文翻译即将推出
Python 3.10 版本采纳了首个 PEP,中文翻译即将推出
113 3
|
Kubernetes 应用服务中间件 调度
Kubernetes 1.6新特性系列 | 高级调度
1、简介 Kubernetes的调度器在大多数情况下能过运行的很好,例如:它能够将Pod调度到有充足资源的Node上;它能够将一组Pod(ReplicaSet, StatefulSet,等)均匀的调度到不同的Node上;它尽力平衡各个节点的资源使用率等。
1422 0
|
2天前
|
云安全 人工智能 自然语言处理
|
6天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
603 16
|
10天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
711 57
Meta SAM3开源:让图像分割,听懂你的话
|
7天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
326 116
|
10天前
|
机器学习/深度学习 人工智能 自然语言处理
AgentEvolver:让智能体系统学会「自我进化」
AgentEvolver 是一个自进化智能体系统,通过自我任务生成、经验导航与反思归因三大机制,推动AI从“被动执行”迈向“主动学习”。它显著提升强化学习效率,在更少参数下实现更强性能,助力智能体持续自我迭代。开源地址:https://github.com/modelscope/AgentEvolver
476 37