使用spring mvc通过链接器链接调用控制器时出现404错误

时间:2018-05-24 22:22:05

标签: spring spring-mvc

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错误。

1 个答案:

答案 0 :(得分:0)

所以有几件事情在我身上跳出来

@RequestMapping(&#34; / hello&#34;)需要一个hello.jsp

如果你想要hellopage:@RequestMapping(&#34; / hellopage&#34;)需要一个hellopage.jsp

位置需要是: javatpoint的\ src \主\ web应用\ WEB-INF \意见\ hellopage.jsp 这是Spring MVC Apps的标准

同时返回新的ModelAndView(&#34; hellopage&#34;,&#34; message&#34;,message); hellopage 是您的目标页面,而不是你好

第二个参数是变量名称,第三个参数是值。 在这种情况下&#34;消息&#34; =消息

这是您的控制器

&#13;
&#13;
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);  
    }  
}
&#13;
&#13;
&#13;

我认为您正在尝试做什么

&#13;
&#13;
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);  
    }  
}
&#13;
&#13;
&#13;

现在,如果你去/ hello,将会呈现hellopage.jsp。

在我的hellopage.jsp 位置非常重要 javatpoint \ SRC \主\ web应用\ WEB-INF \视图\ hellopage.jsp

&#13;
&#13;
<%@ 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>
&#13;
&#13;
&#13;

enter image description here

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