2016-05-20 6 views
0

私はspringmvc.Butを学ぼうとします。私が始めると、私は問題に遭遇しました。Springmvcの制御はjspに値を渡すことができません

私はそれが単なる問題だと思っています。私はインターネット上でそれを検索し、多くの例を見つけます。しかし、私は理由を見つけません。ここにコードがあります。

HomeControll.java

package cn.qingtianr.controll; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 

/** 
* Created by jack on 16-5-20. 
*/ 

@Controller 
public class HomeControll { 

    @RequestMapping("/") 
    public String index(ModelMap model){ 
     String s = "hhh"; 
     model.addAttribute("hello",s); 
     return "index"; 
    } 
} 

index.jspを

<%-- 
    Created by IntelliJ IDEA. 
    User: jack 
    Date: 16-5-20 
    Time: 下午6:31 
    To change this template use File | Settings | File Templates. 
--%> 
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<html> 
<head> 
    <title>Title</title> 
</head> 
<body> 
${hello} 
</body> 
</html> 

のweb.xml

<!DOCTYPE web-app PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd" > 

<web-app> 
    <display-name>Archetype Created Web Application</display-name> 


    <servlet> 
    <servlet-name>hello</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>hello</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

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


    <context:component-scan base-package="cn.qingtianr.controll"/> 


    <mvc:default-servlet-handler/> 


    <mvc:annotation-driven/> 


    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
     <property name="prefix" value="/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 

</beans> 

のpom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>cn.qingtianr</groupId> 
    <artifactId>springmvctest</artifactId> 
    <packaging>war</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>springmvctest Maven Webapp</name> 
    <url>http://maven.apache.org</url> 
    <properties> 
    <spring.version>4.2.5.RELEASE</spring.version> 
    <spring-data.version>1.2.0.RELEASE</spring-data.version> 
    </properties> 
    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>jstl</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 
    </dependencies> 
    <build> 
    <finalName>springmvctest</finalName> 
    </build> 
</project> 

私はindex.jsp.Iは私browser.Maybe上${hello}ないhhhを取得するアクセスすると、私は本当にそれを知らないerror.But低レベルを取得します。

だから誰でも助けてくれますか?ありがとう。

+0

index.jsp – Abdelhak

+2

のパスを制御しようとしている可能性があります[これ]を探している可能性があります(http://stackoverflow.com/questions/2323609/modelattributes-not-accessible-on-jsp) – Apollo

+0

1.どのバージョンあなたは春のですか? 2. Mavenでビルドしていますか? 3.はいの場合、プロジェクトにindex.jspファイルが格納されていますか? 4.存在しない場合は、配備したプロジェクトのディレクトリ構造は何ですか(おそらくあなたのwarファイル内にあります)? – DwB

答えて

1

DispatcherServletマッピングでの古いSpringMVC問題のように見えます。

サーブレットを/にマッピングすると、他のサーブレット(デフォルトのコンテナ1を含む)が解決できないすべてのURLが取得されます。その1つのよくある問題は、Spring MVC DispatcherServletは、空のURL '/'を受け取ることがないということです(この詳細については、answerを参照してください)。

ここでは、コントローラに最初に触れることなくindex.jspファイルを直接呼び出したと仮定します。しかし、(もちろん)モデル変数helloは要求属性に入れられておらず、使用できません。修正方法

を直接JSPファイルを打つ避けるために、グッドプラクティスは、WEB-INFフォルダの下にそれらを置くことをお勧めします。そうすれば、サーブレット・コンテナによってサーバーになることはなく、コントローラから転送された場合にのみ表示されます。> WEB-INF/jsp/index.jspのindex.jspを移動し、jspViewResolver構成の<property name="prefix" value="/WEB-INF/jsp/"/>を変更します。

次に、参照された投稿からソリューションの1つを使用します。あなたは春の設定から<mvc:default-servlet-handler/>を削除xml構成

+0

あなたが言ったように、 '<%@ page isELIgnored =" false "%>'を追加してjspをWEB-INFに移動させれば、動作します。ありがとうございます。 – jack

0

ことは、これを試してみてください:

DOCTYPE

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1" isELIgnored="false"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

値印刷する前にこれを追加します!の

<c:out value="${hello}"></c:out> 

全index.jspを:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1" isELIgnored="false"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <title>Title</title> 
</head> 
<body> 
<c:out value="${hello}" ></c:out> 
</body> 
</html> 

はそれが役に立てば幸いに。

+0

申し訳ありませんが、私はそれを試してみます。しかし、それは動作しません。 – jack

+0

@jackコンソールに何らかのエラーがありますか?私はテストをして、それは私のために働いた。 – kunpapa

+0

これは必須ではありません。 JstlViewには既にコアライブラリが含まれています – DwB

0

を使用しているよう

<mvc:resources mapping="/resources/**" location="/public-resources/"/> 

:私は /*へのDispatcherServletをマッピングすると、例えば使用ResourceHttpRequestHandlerによって処理されたフォルダに静的リソースを移動することをお勧めします。 web.xmlにSpringDispatcherが既にあり、Springの設定でviewリゾルバを持っているので、これは必要ありません。

"/"以外のリクエストマッピングに値を追加します。たとえば、@RequestMapping("/blam")です。 (これのURLはhttp://<host>/<context>/blamになります)。

"index.jsp"をビューファイル名として使用しないでください。 "index.jsp"はデフォルトのウェルカムファイル名です。

これが私の作品:

私のサーブレットの設定で
  1. なし <mvc:default-servlet-handler/>
  2. 私のwarファイルの名前はklabben.war(コンテキストは "klabben")です。
  3. @RequestMapping("/blam")
  4. return "blam.jspインデックスハンドラメソッドです。
  5. index.jspファイルの内容をblam.jspにコピーします。
  6. http://<host>/klabben/blam」というURLを使用して「hello:sss」メッセージを表示します。

注:私は<mvc:default-servlet-handler/>について間違っていました。それは私が思った以外の価値を提供します。

+0

はい。試してみてください。それは私のために働きます。ありがとう。 – jack

関連する問題