Spring - 配置HelloWorld控制器

时间:2017-01-18 17:20:20

标签: spring spring-mvc

我是Spring的新手,我只想转到以下网址http://localhost:8080/DispatcherExample/dispatcher/welcome,转到index.jsp页面。 DispatcherExample是项目的名称,dispatcher是调度程序servlet的url,welcome是映射到控制器方法的url。这是我的课程:

WelcomeController.java

<web-app>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>dispatcher</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>dispatcher</servlet-name>
        <url-pattern>/dispatcher/</url-pattern>
    </servlet-mapping>
</web-app>

的web.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></value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

调度-servlet.xml中

import { Injectable } from '@angular/core';
import { Http, RequestOptions, Headers, URLSearchParams} from '@angular/http'
import { OpenIdTokenRequest, SmartData } from '../model/openid-token-request';
import { OpenIdToken } from '../model/openid-token';
import 'rxjs/add/operator/map';
import 'rxjs/Rx';
import { Observable } from 'rxjs';

@Injectable()

export class TokenService {

    constructor(private _http: Http) { }

    getToken(requestData: OpenIdTokenRequest, smartData: SmartData) {
        let _urlParams = new URLSearchParams();
        _urlParams.append('code', requestData.code);
        _urlParams.append('grant_type', requestData.grantType);
        _urlParams.append('redirect_uri', requestData.redirectUri);
        _urlParams.append('client_id', requestData.clientId);

        let _url = smartData.tokenUri;
        let _options = this.getTokenPostOptions();

        return this._http.post(_url, _urlParams, _options)
            .map(response => <OpenIdToken>response.json())
    }

    private getTokenPostOptions(): RequestOptions {

        let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' });
        let options = new RequestOptions({ headers: headers });

        return options;
    }

}

1 个答案:

答案 0 :(得分:1)

您需要检查两件事。

首先,你的jsp文件的位置。如果它在根文件夹中,则其罚款,否则将jsp文件的位置放在前缀中。

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

其次,如果您的jsp文件位于根文件夹中,则可以使用以下URL访问它。

  

http://localhost:8080/DispatcherExample/welcome

如果您希望您的网址如下

  

http://localhost:8080/DispatcherExample/dispatcher/welcome

然后添加@RequestMapping(&#34; / dispatcher / welcome&#34;)

注意:

  • DispatcherExample是war / project的名称
  • 不要对servlet-name dispatcher感到困惑,它在url中没有贡献,它只是用于在web.xml中识别该servlet

<强>建议:

  • 如果您是新手,请使用基于Spring启动和注释的配置,xml配置现在是一个原始的东西。