Wangjalong
Wangjalong
采纳率66.7%
2020-06-29 22:03

SpringMvc使用RequestMapping写helloworld报404,卡了一天了,救救孩子吧

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
</head>
<body>
<a href="helloworld">helloworld</a>
</body>
</html>

Helloworld.java

package com.pudding.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloWorld {

    @RequestMapping(value = "/helloworld",method = RequestMethod.GET)
    public String hello() {
        System.out.println("hello world");
        return "success";
    }
}

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_2_5.xsd" id="WebApp_ID" version="2.5">
    <!--配置DispatcherSevlet -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!-- 配置DispatcherServlet的一个初始化参数:配置SpringMVC配置文件的位置和名称 -->
        <!-- 实际上也可以不通过contextConfigLocation来配置SpringMVC的配置文件,而使用默认
            默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml
         -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>

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

springmvc.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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 配置自动扫描的包 -->
    <mvc:annotation-driven/>
    <context:component-scan base-package="com.pudding.controller"></context:component-scan>
    <!-- 配置视图解析器  如何把handler方法返回值解析为实际的物理视图-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

succeed.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
</head>
<body>
<h4>success Page</h4>
</body>
</html>

虽然我知道这个很简单,但是真的不知道错在哪里了,卡了一天了,各位大牛救救孩子吧

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • qq_38167579 农夫丶果园 9月前

    你这样直接写 href="helloworld" , 访问的是当前的路径然后把后缀改为helloworld
    看你写的Controller , 如果没有配置context-path的话, 改成href="/helloworld"即可
    如果配置了context-path , 则需要要改为 hrer="/context-path/helloworld" , 但是一般不会直接把context-path写出来
    看你用的应该是jsp, 你可以这样定义一个 ctx 变量 , 然后改成 href="${ctx}/helloworld" 这样去访问

    <c:set var="ctx" value="${pageContext.request.contextPath}"/>
    
    点赞 评论 复制链接分享