0

您好尝试使用 spring 2.6 版本开发简单的 spring 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>

这是我的应用程序 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>

lib文件夹下的jar

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

你能建议我有什么问题吗

4

1 回答 1

2

看起来您错过了添加 ViewResolver 之一。有许多 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
于 2012-07-15T10:41:08.153 回答