首页 > 解决方案 > 使用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 错误。

标签: springspring-mvc

解决方案


所以有几件事突然出现在我身上

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

在此处输入图像描述

希望这有助于随时在评论中提出更多问题。


推荐阅读