0

I want send value from Controller to view jsp but it only show content raw text of jsp similar: "Greeting : ${greeting}"

Can anybody help me to solve this problem to replace "greeting" in jsp to "Hello World from Spring 4 MVC".

Thanks!

This is structure of project

This is structure of project

HelloWorldController.java

@Controller
@RequestMapping("/")
public class HelloWorldController {

    @RequestMapping(method = RequestMethod.GET)
    public String sayHello(ModelMap model) {
        model.addAttribute("greeting", "Hello World from Spring 4 MVC");
        return "welcome";
    }

    @RequestMapping(value = "/helloagain", method = RequestMethod.GET)
    public String sayHelloAgain(ModelMap model) {
        model.addAttribute("greeting", "Hello World Again, from Spring 4 MVC");
        return "welcome";
    }
}

spring-servlet.xml

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

    <context:component-scan base-package="com.hoang.springmvc.controller" />
    <mvc:annotation-driven />

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

</beans>

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>Spring4MVCHelloWorldDemo Web Application</display-name>

   <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
 </servlet>

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

welcome.jsp

<body>
    Greeting : ${greeting}
</body>
Hoang
  • 829
  • 11
  • 18
  • please show your pom.xml and your url http://localhost:8080/... – AchillesVan Aug 25 '16 at 16:59
  • what happens if you just run : http://localhost:8080/ (dont forget the slash after the 8080) – AchillesVan Aug 25 '16 at 17:00
  • you have added the jstl core dependency via Maven ? this is required for Expression Language and once added as a dependency you add the following to the top of your jsp pages: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> – smoggers Aug 25 '16 at 18:10
  • @Georgesvanhoutte: this's my pom.xml http://pastebin.com/L6zcWuZA , and link url: http://localhost:8080/Spring4MVCHelloWorldDemo/ – Hoang Aug 26 '16 at 14:58
  • @smoggers: i also added jstl into pom.xml – Hoang Aug 26 '16 at 15:00
  • @Hoang also your RequestMapping annotations have values of '/' and '/helloagain' so not sure why or how you are navigating to '/Spring4MVCHelloWorldDemo' ? – smoggers Aug 26 '16 at 15:32
  • @smoggers i add dependency you post above but can't fix. when i run project, the browser will open new tab with url default: http://localhost:8080/Spring4MVCHelloWorldDemo/ – Hoang Aug 26 '16 at 16:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121954/discussion-between-smoggers-and-hoang). – smoggers Aug 26 '16 at 16:34

1 Answers1

1

I fix done my problem by add <%@ page isELIgnored="false" %> to my top jsp.

This solution by: https://stackoverflow.com/a/1530422/4768616

Community
  • 1
  • 1
Hoang
  • 829
  • 11
  • 18
  • Nice one. I have had got this same problem and this solution worked for me. Completely surprising, I have made dozens of pages and I haven't had any problem like this. Upvoted! – Alex Apr 03 '18 at 21:02