-1

Netbeans 7.3 で簡単な helloworld を作成しました。しかし、コントローラーからの値でjspをロードする際に問題があります。http://www.tutorialspoint.com/spring/spring_tutorial.pdf (page 141.) の説明を部分的に参考にしています。しかし、コントローラに設定した値でjspをロードしたいのですが、値が表示されません。コントローラーには、キャプチャ GET 要求があります。この URL を入力しました: http://localhost:8080/HelloWorld3/hello.htm。jspを取得しましたが、値が表示されません。上記のチュートリアルで書いた URL を入力すると http://localhost:8080/HelloWeb/hello、404 エラーが発生します。このページは見つかりません。どの URL を入力する必要がありますか、それとも悪いことをしていますか? そのエラーはいくつかの設定ファイルにあると思います。どうも

applicationContext.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.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="cz.ryska.controllers"/>

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

dipatcher-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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

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

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
                <prop key="hello.htm">helloController</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" />

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

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>2</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>

コントローラ:

@Controller
@RequestMapping("/hello") 
public class HelloController {

    @RequestMapping(method = RequestMethod.GET) 
    public String printHello(ModelMap model) {
        model.addAttribute("message", "Hello Spring MVC Framework!"); 
        return "hello"; 
    }
}

hello.jsp:

<%@page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello!!!!!!</h1>
        <h2>${message}</h2>
    </body>
</html>
4

2 に答える 2

0

helloWorld アプリケーションをこのようにコーディングした特定の理由はありますか? 単純な helloWorld アプリケーションの場合、必要以上に複雑に見えます。

1 つには、そうする特定の理由がない限り、実際には 2 つの別個のコントローラーを定義する必要はありません。

リンクしたチュートリアルポイントの例にも取り組みましたが、私のものは完全に機能しましたが、元の例のクローンでした。

例を拡張したり試したりしたい場合は、最初に例を複製して動作させることをお勧めします...そして、そこから一歩ずつ進んでください。

目の前に解決策がありませんが、Georgy Gobozov の答えは正しいようです。

于 2013-06-14T03:19:26.953 に答える