0

私は春のmvcを学ぼうとしていて、問題に直面しています(これは一般的な問題のようです)。私は多くの解決策を検索しましたが、何も助けてくれません...

私のweb.xmlは次のとおりです。

   <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring Hello World</display-name>
<welcome-file-list>
    <welcome-file>hello.jsp</welcome-file>
</welcome-file-list>

<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

<servlet>
    <servlet-name>chatbooster</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>chatbooster</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

私のchatbooster-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: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="com.controller" />

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

hello.jsp を実行しようとすると、要求されたリソースが利用できないというエラーが表示されます。

Hello.jsp:

   <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="i" uri="http://java.sun.com/jsp/jstl/core" %> 
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     
 <html>
<head>
<title>Home</title>
</head>
<body>
 <h1>Hello World!</h1>

 <hr/>

  <form action="hi">
   Name: <input type="text" name="name"> <input type="submit" value="Submit">
   </form>

 </body>
 </html>

HelloWorldController.ava

 @Controller
public class HelloWorldController {

@RequestMapping("/")
 public String hello() {
 return "hello";
 }

@RequestMapping(value = "/hi", method = RequestMethod.GET)
 public String hi(@RequestParam("name") String name, Model model) {
 String message = "Hi " + name + "!";
 model.addAttribute("message", message);
 return "hi";
}

}

編集1:

私の単純なhtmlページも実行されておらず、同じ例外がスローされているため、Tomcatサーバーが原因で問題が発生しています。Tomcat サーバー バージョン 7 を使用しています。この例外の原因を教えてもらえますか?

4

3 に答える 3

1

hello.jsp は WEB-INF 内にあるため、Tomcat は hello.jsp を認識しません。

変化する

<welcome-file-list>
    <welcome-file>hello.jsp</welcome-file>
</welcome-file-list>

<welcome-file-list>
    <welcome-file>/</welcome-file>
</welcome-file-list>

それが動作します。

于 2012-12-19T21:23:21.180 に答える
0

みなさん、ありがとうございました。実際、htmlページとjspページの場所をwebinfフォルダーからwebcontentフォルダーに変更するだけで、問題を解決することができました。なぜそれが機能したのかわかりませんが、ページは現在サーバーによって実行されています。

于 2012-12-20T17:19:26.233 に答える
0

これは、Hello.jsp のケースである可能性があります。Hello.jsp の最初の文字は大文字ですが、コントローラーでは小文字の hello を返しています。文字列「Hello」を返すようにコントローラーを変更すると、動作するはずです。

于 2012-12-19T21:24:36.340 に答える