弹簧mvc代码当我点击链接时给出404错误

时间:2022-11-29 15:40:56

index.jsp:

 <a href="hello">click</a>`

HelloWorldController:

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 ";
    return new ModelAndView("hellopage","message",message);
}

}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>springmvc</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet> 
    <servlet-name>spring</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup> 
</servlet> 

    <servlet-mapping>
        <servlet-name>spring</servlet-name> 
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

spring-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:p="http://www.springframework.org/schema/p"  
xmlns:context="http://www.springframework.org/schema/context"  
xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.javatpoint"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>
</beans>

弹簧mvc代码当我点击链接时给出404错误

hellopage.jsp:

Message is: ${message}

弹簧mvc代码当我点击链接时给出404错误

1 个解决方案

#1


0  

Try using this

试试这个

 <a href="/hello">click</a>

As controller is mapped with URL "/hello"

控制器使用URL“/ hello”映射

#1


0  

Try using this

试试这个

 <a href="/hello">click</a>

As controller is mapped with URL "/hello"

控制器使用URL“/ hello”映射