spring - 使用spring mvc通过hypher链接调用控制器时出现404错误
问题描述
The below code is controller. At the time of invoking controller we are getting 404 error. Please help me how to resolve it.
package com.javatpoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "HELLO SPRING MVC HOW R U";
return new ModelAndView("hellopage", "message", message);
}
}
This is my jsp.
<%@ page isELIgnored="false" %>
<a href="hello.html">click</a>
如何使用spring mvc调用spring控制器。
Please help on this?
我们想知道为什么会收到 404 错误。
解决方案
所以有几件事突然出现在我身上
@RequestMapping("/hello") 需要一个 hello.jsp
如果你想要 hellopage:@RequestMapping("/hellopage") 需要一个 hellopage.jsp
位置需要是:javatpoint\src\main\webapp\WEB-INF\views\hellopage.jsp 这是Spring MVC Apps的标准
还返回 new ModelAndView("hellopage", "message", message); hellopage是你的目标页面这里不是你好
第二个参数是变量名,第三个是值。在这种情况下,“消息”=消息
这是你的控制器
package com.javatpoint.app;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
* Handles requests for the application home page.
*/
@Controller
public class HelloWorldController {
private static final Logger logger = LoggerFactory.getLogger(HelloWorldController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/hellopage", method = RequestMethod.GET)
public ModelAndView helloWorld() {
String message = "HELLO SPRING MVC HOW R U";
return new ModelAndView("hellopage", "message", message);
}
}
我认为你正在尝试做的事情
package com.javatpoint.app;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
* Handles requests for the application home page.
*/
@Controller
public class HelloWorldController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public ModelAndView helloWorld() {
String message = "HELLO SPRING MVC HOW R U";
return new ModelAndView("hellopage", "message", message);
}
}
现在如果你去 /hello hellopage.jsp 将被渲染。
在我的 hellopage.jsp位置是关键 javatpoint\src\main\webapp\WEB-INF\views\hellopage.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Hello Page</title>
</head>
<body>
<%@ page isELIgnored="false" %>
<P> ${message}. </P>
</body>
</html>
希望这有助于随时在评论中提出更多问题。
推荐阅读
- reactjs - antd v4.2.5使用showLine时如何隐藏文件图标
- matrix - OpenGL ES 2.0 - 围绕枢轴点 2D 旋转点(顶点着色器)
- c++ - 即使出现 0 个错误,Getter 似乎也会使程序崩溃
- macos - 我的 Roblox Studio 脚本有时会出错.. idk 为什么
- python - TypeError:调用函数时,'str'对象不可调用
- nginx - nginx在重写规则中将大写字母重写为小写字母
- python - HTTPResponse 对象没有属性 json
- python - django ImportError:无法导入名称“连接”
- javascript - 如何连接到firebase中文档的字符串字段
- swift - 是否可以反转 tableView 的顺序?我搜索了很多解决方案,但没有任何效果