2

私はSpringが初めてで、数え切れないほどの例を見てきましたが、この問題を理解できません。私が抱えている問題は、index.jsp ページからリンクをクリックしても、hello.jsp ページに転送されないことです。何故かはわからない。私は 2 日間かけて例を調べ、無数の例を試して問題の原因を特定しようとしましたが、役に立ちませんでした。何が問題なのか教えていただければ幸いです。NetBeans 7.2、Tomcat 7、JDK 7、Spring 3.2.1 を使用しています。redirect.jsp は index.jsp ページにアクセスするために機能しますが、index.jsp ページのリンクをクリックすると 404 エラーが発生します。

私が理解している方法では、ウェルカムファイルはredirect.jspです(このファイルがどのように処理されるのか、なぜディスパッチャーを介して処理されないのかわかりません-それはjspだからですか?)。これにより、htm ファイルにリダイレクトされます。ディスパッチャーは、index.htm に一致する URL マッピングを見つけ、indexController を呼び出します。indexController は、"/WEB-INF/jsp/" + index + ".jsp" である "index" ビューに送信します。

Tomcat が起動すると、次のメッセージが表示されます。

Mapped URL path [/helloworld] onto handler 'helloWorldController'
Mapped URL path [/helloworld/*] onto handler 'helloWorldController'
Mapped URL path [/index.htm] onto handler 'indexController'

index.jsp のリンクをクリックすると、次の警告メッセージが表示されます。

No mapping found for HTTP request with URI [/WTSpring3/hello.htm]

私のWebファイルは次のようにレイアウトされています:

web
  WEB-INF
    jsp
      hello.jsp
      index.jsp
    applicationContext.xml
    dispatcher-servlet.xml
    web.xml
  redirect.jsp

リダイレクト.jsp

<% response.sendRedirect("index.htm"); %>

index.jsp (これは私が作業に取り掛かることができないものです。Spring セットアップのリンクが間違っているかどうかはわかりません)。

<html><body>
<a href="hello.htm">Say Hello</a>
</body></html>

hello.jsp

<html>
<body>${message}</body>
</html>

web.xml - ここでの URL パターンは *.htm であることに注意してください。これは、index.jsp で使用するものです。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"    
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

dispatcher-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"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:context="http://www.springframework.org/schema/context"

   xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

<context:component-scan base-package="net.mvp.spring3.controller" />

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

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />

</beans>

HelloWorldController.java

 package net.mvp.spring3.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.portlet.ModelAndView;

    @Controller
    public class HelloWorldController {

        @RequestMapping("hello")
        public ModelAndView helloWorld() {
            String message = "Hallo Asgard";
            return new ModelAndView("hello", "message", message);
        }
    }
4

3 に答える 3

1

新しい Bean を dispatcher-servlet.xml に追加します

<bean id="helloWorldController" 
    class="net.mvp.spring3.controller.HelloWorldController" />

Bean を id urlMapping に置き換え、以下のコードを使用します

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

注:変更する必要はありません@RequestMapping("hello")

于 2013-02-19T03:58:50.203 に答える
0

これを行うにはおそらく他の方法がありますが、これは私がビューBeanを使用してそれを学んだ方法です。何が起こるかは次のとおりです。

  1. ユーザーが特定の名前のリンクをクリックします。例:www.yoursite.com/ hello
  2. springはrequestmappingsで' hello 'を探します。
  3. requestmappingが見つかった場合、対応するメソッドが実行されます。
  4. このメソッドは、jspファイルと同じ場所にあるviews.xmlで定義する必要があるurl(WEB-INF / jsp / hello.jsp)のエイリアス( hello )を含むModelAndViewを返します。
  5. 最後に、エイリアスhelloのurl(WEB-INF / jsp / hello.jsp)のページがユーザーに表示されます。

views.xmlは、さまざまなjspファイルのURLのエイリアスを定義します。したがって、そのファイルでは、helloはWEB-INF / jsp/hello.jspに対応すると言います。

それはこれらの線に沿って何かに見えます:

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

<bean id="excelViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="order" value="1"/>
    <property name="location" value="/WEB-INF/views.xml"/>
</bean>

<!-- in views.xml -->

<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="hello" class="org.springframework.web.servlet.ModelAndView">
    <property name="hello" value="/WEB-INF/jsp/hello.jsp" />
</bean>

</beans>

コントローラのリクエストマッピングは同じように見えます。

@Controller
public class HelloWorldController {

    @RequestMapping("hello") // what the user calls for, the request
    public ModelAndView helloWorld() {
        String message = "Hallo Asgard";
        return new ModelAndView("hello", "message", message); // the response
        //"hello" in ModelAndView is the same "hello" as the one in views.xml
    }
}

それがお役に立てば幸いです。

于 2013-02-18T22:22:18.713 に答える
0

hello.htm ファイル名を hello2.jsp などに変更できますか? つまり、リクエスト マッピングとは別の名前を使用してみてください。

@Controller
public class HelloWorldController {

    @RequestMapping("hello") // what the user calls for, the request
    public ModelAndView helloWorld() {
        String message = "Hallo Asgard";
        return new ModelAndView("hello2", "message", message); // the response
        //"hello" in ModelAndView is the same "hello" as the one in views.xml
    }
}

hello2.jsp

<html>
<body>${message}</body>
</html>
于 2013-02-19T07:49:22.527 に答える