#SpringMVC:如何给首页值参 #首页无法传参 #首页控制器 @FDDLC
发布日期:2021-06-30 21:05:47 浏览次数:3 分类:技术文章

本文共 1312 字,大约阅读时间需要 4 分钟。

一、问题

无法给index.jsp值参。index.jsp的位置(直接位于webapp下):

补充信息:

index.jsp页面的核心内容:

controller的核心内容:

访问首页时参数没有值,页面一片空白:

 

二、分析

Tomcat的自带的web.xml中规定了welcome-file-list:

因为上面这个东东的存在,当你访问项目根目录(比如localhost:8080/)时,如果Tomcat发现存在webapp/index.*,那么它就会直接把index.*返回!

在这种情况下,就算你配置了,也根本不会被触发!

知道了原因,解决的思路就很多了。

比如:把welcome-file-list给删了(亲测有效)。

比如:把webapp目录下的index.jsp、index.html、index.htm删了!当然,将它们名字改掉也OK。

 

三、解决方案

下面是一个示例:

1、项目结构:

 

2、MyController.java:

package cn.fddlc.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class MyController {    @RequestMapping("/")    public String home(Model model){        model.addAttribute("name","Tom");        model.addAttribute("age",20);        return "home.jsp";    }}

 

3、home.jsp:

<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" %>    首页

${name}

${age}

 

4、访问结果:

 

附a:application.xml:

 

附b:web.xml:

Archetype Created Web Application
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:application.xml
dispatcherServlet
/

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载地址:https://liuxingchang.blog.csdn.net/article/details/117026741 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:#Chrome:访问iframe中的元素报错:single.html:13 Uncaught DOMException: Blocked a frame … @FDDLC
下一篇:#图片上传、实时预览(无刷新提交表单):超简单的实现 @FDDLC

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月08日 02时35分38秒