HelloSpringMVC配置版实现步骤解析

网友投稿 202 2022-11-24


HelloSpringMVC配置版实现步骤解析

配置版步骤

新建一个module,添加web的支持

确定导入了SpringMVC的依赖

配置web.xml,注册DispatcherServlet

xmlns:xsi="http://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">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc-servlet.xml

1

springmvc

/

xmlns:xsi="http://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">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc-servlet.xml

1

springmvc

/

编写SpringMVC的配置文件!名称:springmvc-servlet.xml

添加 处理映射器

添加 处理器适配器

添加视图解析器

编写我们要操作业务Controller,要么实现Controller接口,要么增加注解;需要返回一个ModelAndView,装数据,封视图;

public class HelloController implements Controller {

public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

ModelAndView mv = new ModelAndView();

//1.业务代码

String result="HelloSpringMVC";

mv.addObject("msg",result);

//2.视图跳转

mv.setViewName("test");

return mv;

}

}

将自己的类交给SpringIOC容器,注册bean

写要跳转的jsp页面,显示ModelandView存放的数据,以及页面;

<%--

Created by IntelliJ IDEA.

User: Administrator

Date: 2020/8/1

Time: 23:54

To change this template use File | Settings | File Templates.

--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

${msg}

配置Tomcat启动测试!

可能遇到的问题:访问出现404,排查步骤:

查看控制台输出,看是不是缺少了jar包;

如果jar包存在,显示无法输出,就在idea的项目发布中,添加lib依赖!

重启Tomcat即可解决!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java中使用Properties配置文件的简单方法
下一篇:springboot2.3 整合mybatis
相关文章

 发表评论

暂时没有评论,来抢沙发吧~