02.使用注解进行创建SpringMVC

02.使用注解进行创建SpringMVC

首先需要注意是否在当前的模块下创建以及导入了lib目录

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dByov97q-1665200817873)(C:\Users\86135\AppData\Roaming\Typora\typora-user-images\image-20220802103845343.png)]

注意需要使用文件过滤

<build>
    <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>

一、配置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_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!-- 启动顺序,数字越小,启动越早 -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--所有请求都会被springmvc拦截 -->
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

二、编写SpringMVC配置文件

<?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"
       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//mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--自动扫描包,让springmvc不处理静态资源,支持mvc注解驱动-->
    <context:component-scan base-package="com.yume.controller"/>
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

三、编写Controller

package com.yume.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/qwq")
public class DiamondController {

    //访问地址后缀为 /qwq/diamond
    @RequestMapping("/diamond")
    public String diamond(Model model){
        String text = "我永远喜欢钻石";
        model.addAttribute("msg",text);
        // WEB-INF/jsp/thisIsJspName.jsp
        return "thisIsJspName";
    }
}
addAttribute("msg",text);
        // WEB-INF/jsp/thisIsJspName.jsp
        return "thisIsJspName";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 首先,需要在本地安装Maven。可以从官网下载Maven并按照说明进行安装。 2. 创建一个Maven项目。可以使用Maven的命令行工具或者使用Eclipse等IDE来创建。 3. 在项目的pom.xml文件中添加Spring MVC的依赖。可以在Maven仓库中找到相应的依赖,例如: ``` <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency> ``` 4. 配置Spring MVC。在项目的src/main/resources目录下创建一个名为springmvc-servlet.xml的配置文件,并添加以下内容: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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> ``` 其中,context:component-scan用于扫描控制器类的包路径,mvc:annotation-driven用于启用Spring MVC的注解驱动,InternalResourceViewResolver用于配置JSP视图解析器。 5. 创建控制器类。在项目的src/main/java目录下创建一个名为com.example.controller的包,并在该包下创建一个控制器类,例如: ``` @Controller public class HelloController { @RequestMapping("/hello") public String hello(Model model) { model.addAttribute("message", "Hello, Spring MVC!"); return "hello"; } } ``` 其中,@Controller用于标识该类为控制器类,@RequestMapping用于映射请求路径,Model用于向视图传递数据。 6. 创建JSP视图。在项目的src/main/webapp/WEB-INF/views目录下创建一个名为hello.jsp的JSP文件,例如: ``` <!DOCTYPE html> <html> <head> <title>Hello, Spring MVC!</title> </head> <body> <h1>${message}</h1> </body> </html> ``` 其中,${message}用于显示控制器类中传递的数据。 7. 运行项目。可以使用Maven的命令行工具或者使用Eclipse等IDE来运行项目。在浏览器中输入http://localhost:808/项目名/hello,即可看到Hello, Spring MVC!的输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值