in dynamic web project spring mvc framework ${} not showing variable value in jsp page.
please find below code:
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="webapp_id" version="3.1"> <display-name>springmvctest</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
dispatcher-servlet
<?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:context="http://www.springframework.org/schema/context" 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"> <context:component-scan base-package="com.springtutorial.basecontroller" /> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix"> <value>/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
basecontroller.java controller class
package com.springtutorial.basecontroller; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.servlet.modelandview; @controller public class basecontroller { @requestmapping(value="/", method=requestmethod.get) public modelandview printhello(){ return new modelandview("index", "message", "hi"); } }
index.jsp jsp page
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>spring mvc framework</title> </head> <body> <h1>learning</h1> <h2>${message}</h2> </body> </html>
i new spring mvc framework, started learning it. checked on internet code , similar, still no luck.
please let me know if missing something.
thanks
i made changes in basecontroller.java file , dispatcher-servlet.xml file:
basecontroller.java
package com.springtutorial.basecontroller; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; //import org.springframework.web.bind.annotation.requestmethod; //import org.springframework.web.servlet.modelandview; @controller public class basecontroller { @requestmapping("/") public string index(model model){ model.addattribute("message", "hi"); return "index"; } }
dispatcher-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:context="http://www.springframework.org/schema/context" 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"> <context:component-scan base-package="com.springtutorial.basecontroller" /> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix"> <value>/web-inf/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
please try this. working me
Comments
Post a Comment