简体   繁体   English

HTTP状态404。请求的源不可用

[英]HTTP Status 404. The Requested Source Not Available

I'm still new with java and spring, currently I'm trying to map "/buku/" to my home.jsp, but it's doesnt seems to work. 我对java和spring还是很陌生,目前我正在尝试将“ / buku /”映射到我的home.jsp,但这似乎不起作用。

HomeController.java HomeController.java

package com.web.buku;

import java.security.Principal;
import org.apache.log4j.Logger;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.ui.ModelMap;  


@Controller
public class HomeController {

    private static Logger logger =Logger.getLogger(HomeController.class);

    @RequestMapping("/")
    public String home(ModelMap model){

        model.addAttribute("message", "Hello Spring MVC Framework!");
        return "home";
    }
}

home.jsp home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${message}
yosh
</body>
</html>

web.xml 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <display-name>buku</display-name>
    <description>Database of Buku</description>
    <servlet>
        <servlet-name>book</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>book</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/book-servlet.xml</param-value>
    </context-param>

    <filter>
        <filter-name>encoding-filter</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encoding-filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <session-config>
        <session-timeout>120</session-timeout>
    </session-config>

</web-app>

book-servlet.xml book-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   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.web.buku" />

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

</beans>

I run my tomcat server and trying to access localhost:8080/buku/ and it won't work, keep giving me "HTTP Status 404 - /buku/" error 我运行我的tomcat服务器并尝试访问localhost:8080 / buku /,它将无法正常工作,不断给我“ HTTP状态404-/ buku /”错误

try adding <mvc:annotation-driven /> in your book-servlet.xml to scan the controllers 尝试在book-servlet.xml中添加<mvc:annotation-driven />来扫描控制器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   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
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

   <context:component-scan base-package="com.web.buku" />
   <mvc:annotation-driven />
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

don't forget to add mvc namespace 不要忘记添加mvc名称空间

Update: I have also notice that you're not specifying your request method. 更新:我还注意到您没有指定请求方法。 replare @RequestMapping("/") with @RequestMapping(value="/",method=RequestMethod.GET) @RequestMapping(value="/",method=RequestMethod.GET)替换@RequestMapping("/") @RequestMapping(value="/",method=RequestMethod.GET)

尝试删除方法上方的requestMapping注释-主页?

You want to map "/book/home" to home.jsp ? 您想将"/book/home"映射到home.jsp吗? I think you intend to map the /book/home path to your HomeController 's home method... Then try the below 我认为您打算将/ book / home路径映射到HomeControllerhome方法...然后尝试以下操作

@Controller
public class HomeController {

    private static Logger logger =Logger.getLogger(HomeController.class);

    @RequestMapping("book/home")
    public String home(ModelMap model){

        model.addAttribute("message", "Hello Spring MVC Framework!");
        return "home";
    }
}

Now when you hit the URL: localhost:8080/buku/book/home - you should get the home.jsp displayed 现在,当您访问URL时: localhost:8080/buku/book/home您应该显示home.jsp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM