0

I try to learn springmvc.But When I get to start,I encountered a problem.

I think it is a simply problem.And I search for it on internet and find a lot of examples.But i don't find the reason.Here is the code.

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>

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

When I access the index.jsp.I get the ${hello} not hhh on my browser.Maybe I get a low level error.But I don't know it really.

So can anyone help me?Thanks.

jack
  • 5
  • 7
  • try to control the path of index.jsp – Abdelhak May 20 '16 at 12:12
  • 2
    May be you are looking for [this](http://stackoverflow.com/questions/2323609/modelattributes-not-accessible-on-jsp) – Akhil May 20 '16 at 12:17
  • 1. what version of spring are you using? 2. Are you building with Maven? 3. If yes, where is the index.jsp file stored in your project? 4. if no, what is the directory structure of your deployed project (perhaps in your war file)? – DwB May 20 '16 at 12:36
  • When i add `xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"` , I even can't get the ${hello}.I get nothing. – jack May 20 '16 at 12:39

3 Answers3

1

Looks like the good old SpringMVC problem with DispatcherServlet mapping.

You mapped the servlet to /, meaning that it will get all the URL that no other servlet (including default container one) could resolve. One common issue with that is that Spring MVC DispatcherServlet will never receive the empty URL '/' (see this other answer of mine for a more detailed explaination).

So I assume that you directly called the index.jsp file without first hitting the controller. But then (of course) the hello model variable has not been put in request attributes and cannot be used.

How to fix:

To avoid directly hitting the jsp files, good practices recommend to put them under WEB-INF folder. That way, they will never be server by the servlet container, and will only be displayed if forwarded to from a controller => move index.jsp under WEB-INF/jsp/index.jsp and accordingly change <property name="prefix" value="/WEB-INF/jsp/"/> in jspViewResolver configuration.

Then use one of the solutions form the referenced post. I advise to map the DispatcherServlet to /* and to move static resources to a folder processed by a ResourceHttpRequestHandler using for example:

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

as you are using xml configuration

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • I add `<%@ page isELIgnored="false" %>` and move the jsp to the WEB-INF as you say,then it works.Thank you. – jack May 20 '16 at 13:09
0

Try this:

Add this before !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" %>

To print a value:

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

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

Hope it helps.

kunpapa
  • 367
  • 1
  • 9
  • @jack Some kind of error on console? I made a test and it worked for me. – kunpapa May 20 '16 at 12:42
  • This is not required. the JstlView already includes the core lib – DwB May 20 '16 at 12:52
  • I add the code in the place which you said in the index.jsp.And i run the project and I can't even get the ${hello},I get nothing.And there is no exception. – jack May 20 '16 at 12:52
  • Check the post again, I post the entire index.jsp. – kunpapa May 20 '16 at 12:55
  • I add <%@ page isELIgnored="false" %> and move the jsp to the WEB-INF as Serge Ballesta say,and then it works.And also thank you. – jack May 20 '16 at 13:12
0

Remove <mvc:default-servlet-handler/> from your spring configuration. You don't need it because you already have the SpringDispatcher in your web.xml and have a view resolver in your spring config.

Add a value to the request mapping other than "/". for example, @RequestMapping("/blam"). (the url for this will be http://<host>/<context>/blam).

Don't use "index.jsp" as your view file name. "index.jsp" is the default welcome file name.

This works for me:

  1. no <mvc:default-servlet-handler/> in my servlet config.
  2. My war file is named klabben.war (the context is "klabben").
  3. @RequestMapping("/blam").
  4. return "blam.jsp from the index handler method.
  5. copy your index.jsp file content to blam.jsp.
  6. use the url "http://<host>/klabben/blam" to display the "hello: sss" message.

Note: I was wrong about <mvc:default-servlet-handler/>. It provides value other than I thought.

DwB
  • 37,124
  • 11
  • 56
  • 82