2012-07-15 84 views
0

想要使用spring 2.6版本開發簡單的彈簧mvc應用程序。簡單彈簧mvc應用程序找不到頁面

這是我的web.xml文件內容。

<?xml version="1.0" encoding="UTF-8"?> 

<web-app version="2.4" 
     xmlns="http://java.sun.com/xml/ns/j2ee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > 

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

    <servlet-mapping> 
    <servlet-name>springtutorial</servlet-name> 
    <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 

    <welcome-file-list> 
    <welcome-file> 
     index.jsp 
    </welcome-file> 
    </welcome-file-list> 

</web-app> 

這是lib文件夾下,我的應用程序servlet配置文件 「springtutorial-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" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 

    <bean id="helloController" class="com.net.technicalkeeda.controller.HelloController" /> 

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
     <property name="mappings"> 
      <props> 
       <prop key="hello.htm">helloController</prop> 
      </props> 
     </property> 
    </bean> 

</beans> 

j2ee.jar 
jstl.jar 
spring.jar 
spring-webmvc.jar 

這是我的控制器文件

包COM .net.technicalkeeda.controller;

import java.io.IOException; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.Controller; 

public class HelloController implements Controller { 

    public ModelAndView handleRequest(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException { 

     System.out.println("I am here"); 

     return new ModelAndView("hello.jsp"); 
    } 

} 

在Tomcat中無法通過URL訪問項目部署後

http://domainname.com/springtutorial/hello.htm

能否請你建議我有什麼問題

回答

2

看起來像你錯過了添加的一個視圖解析器。有很多ViewResolver可用,這是你可以使用的其中之一。

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

僅僅指定

return new ModelAndView("hello") 

ModelAndView mav=new ModelAndView(); 
mav.setViewName("hello"); 
return mav