2014-12-04 42 views
0

我還是新的Java和春天,目前我試圖映射「/ buku /」到我的home.jsp,但它似乎does not工作。HTTP狀態404請求的源不可用

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

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

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

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

我跑我的Tomcat服務器,並嘗試訪問本地主機:8080 /不哭/和它不會工作,不斷給我「HTTP狀態404 - /不哭/」錯誤

回答

0

嘗試在你的書,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> 

不要忘記添加MVC命名空間

更新:我有ALS o注意你沒有指定你的請求方法。 replare @RequestMapping("/")@RequestMapping(value="/",method=RequestMethod.GET)

+0

感謝您的回覆,但它仍然不能解決問題 – jourpool 2014-12-04 07:34:54

+0

請看我的更新回答 – user3308224 2014-12-04 07:52:54

+0

試過了,仍然沒有解決問題 – jourpool 2014-12-04 08:19:03

0

嘗試刪除requestMapping註釋上面的方法 - home?

+0

嘗試過它,不工作 – jourpool 2014-12-04 05:25:09

+0

啓動Web服務器時是否顯示任何錯誤?因爲你甚至不能訪問項目基礎根路徑。 – Kenti 2014-12-04 05:37:42

+0

沒有錯誤。仍然不知道原因是什麼 – jourpool 2014-12-04 09:30:01

0

要將地圖"/book/home"設置爲home.jsp? 我想你打算映射/電子書/ 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"; 
    } 
} 

現在,當你點擊的網址:localhost:8080/buku/book/home - 你應該得到的home.jsp顯示

+0

我很抱歉,我的意思是將'/ buku /'映射到home.jsp – jourpool 2014-12-04 07:27:43

+0

@jourpool - 您是否嘗試使用URL:localhost:8080/buku – Arun 2014-12-04 08:20:36

+0

當然,當然是的 – jourpool 2014-12-04 08:26:12

相關問題