【问题标题】:Spring - configuring a simple dispatcher servlet with a controllerSpring - 使用控制器配置一个简单的调度程序 servlet
【发布时间】:2017-01-17 18:11:35
【问题描述】:

我是 Spring 新手,我试图用控制器配置一个简单的调度程序 servlet,但我似乎无法让它工作。我想去http://localhost:8080/1/dispatcher/index 在控制台中打印出“欢迎”。我的老板说,由于映射,它还应该自动将您重定向到 index.jsp 页面。

WelcomeController.java

@Controller
public class WelcomeController {

    @RequestMapping("/index")
    public ModelAndView welcome()
    {
        System.out.println("welcome entered");


    }
}

web.xml

<web-app>
  <display-name>Archetype Created Web Application</display-name>
 <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

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

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/dispatcher/</url-pattern>
    </servlet-mapping>
</web-app>

springDispatcherServlet-servlet.xml

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

   <context:component-scan base-package="com.paymon" />
<mvc:annotation-driven />

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

【问题讨论】:

    标签: spring servlets


    【解决方案1】:

    首先,指示的代码似乎无法编译。 welcome() 方法应该返回一个 ModelView 类型的对象,指示要显示的视图。

    在这个方法中你可以把视图放到resolver中,如果你想显示index.jsp,你把index放到viewName方法中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 1970-01-01
      • 2016-08-02
      相关资源
      最近更新 更多