Spring: bean with scope=session in jsp - spring

In my Spring MVC project I can't access to the parameters of bean with scope=session.Into Http session there is a bean with name "scopedTarget.user" .I want to print, in jsp page, the User's name.Why is it so difficult to access to this parameters?Where am I wrong?
ControllerHome:
#Controller
public class ControllerHome {
#Autowired
private User user;
#RequestMapping(value="/",method=RequestMethod.GET)
public String welcome(ModelMap model){
model.addAttribute("utente", user);
return "index";
}
#RequestMapping(value="/add",method=RequestMethod.GET)
public String add(#ModelAttribute("utente") User utente,HttpServletRequest request){
HttpSession session=request.getSession();
Enumeration<String> list=session.getAttributeNames();
while(list.hasMoreElements())
System.out.println(list.nextElement());
return "redirect:/";
}
}
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form:form method="get" action="add" modelAttribute="utente">
<form:input path="nome"/>
<input type="submit" value="submit">
</form:form>
<c:out value="${sessionScope['scopedTarget.user'].nome}"/>
</body>
</html>
User.java:
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String nome;
public User(){}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>config</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>config</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
config-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
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-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="controller"/>
<mvc:annotation-driven/>
<bean class="coreservlets.User" id="user" name="user" scope="session">
<aop:scoped-proxy/>
</bean>
</beans>

If you add these anotations #Component and #Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) to the POJO you want to be managed in a session, you can then use it in the JSP using the method you spoke of in your question (and Autowire it in your controllers). Make sure of two things though:
Don't override the toString(), otherwise it's that result that will be assigned to ${sessionScope['scopedTarget.user'].
Your POJO, in this case User, needs to be "caught" by the Spring's component scanner

Related

what is wrong with my Spring MVC 3.0 configuration?

i am a newbie in the Java EE Spring MVC coding area. When i configured my first Spring MVC 3.0 site, i got a strange question that i have to manually type the MVC named url route to make it work.
The complete url route in my example is:
http://localhost:8080/SpringMVC/hello.jsp
i wanted to send a word to the controller and display it on the view.
But when i click return, the error page said:
HTTP Status 404 - /hello.do
type Status report
message /hello.do
description The requested resource is not available.
Apache Tomcat/7.0.85
So the url route was then: http://localhost:8080/hello.do
And i have to type the complete route :
http://localhost:8080/SpringMVC/hello.do to make it work.
i think there must be some errors in my web.xml and SpringMVC-servlet.xml configurations. i post all of my code below and welcome any suggestions.
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMVC</display-name>
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
SpringMVC-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 配置上传文件的参数 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="209715200" />
<property name="defaultEncoding" value="UTF-8" />
<property name="resolveLazily" value="true" />
</bean>
<!-- 配置Controller -->
<bean name="/hello.do" class="com.yyy.controller.HelloController"></bean>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
HelloController.java:
package com.yyy.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class HelloController extends AbstractController{
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String hello = request.getParameter("hello");
System.out.println("------:" + hello);
ModelAndView mav = new ModelAndView("index");
mav.addObject("helloworld", "hello "+hello);
return mav;
}
}
hello.jsp
<%# page language="java" contentType="text/html; charset=utf-8"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<form action="hello.do" method="post">
hello:<input type="text" name="hello"/>
<input type="submit" value="submit">
</form>
</body>
</html>
And the index.jsp:
<%# page language="java" contentType="text/html; charset=utf-8"
pageEncoding="ISO-8859-1"%>
<!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>index.jsp</title>
</head>
<body>
<h1>${helloworld} </h1>
</body>
</html>
You can change the <form action> like below. This is a better approach.
<form action="${pageContext.request.contextPath}/hello.do">

Spring MVC page not found

I am a newbie to Spring framework especially Spring MVC.
I am writing a code which takes in a Name in a form and displays it on the page. So index.jsp is the form class in which on hitting the sayHello button the request should get forwarded to another page hello.jsp and prints the message. But on clicking the button it is giving 404 error. According to me all the names and configurations are fine but it just doesn't go and prints the message.
index.jsp
<h2>Form</h2>
<form action="./hello.html">
Name: <input type="text" name="name"/><br>
<input type="submit" value="sayHello"/>
</form>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>HelloWorld</display-name>
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
HelloController.java
package com.spring.mvc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HelloController {
#RequestMapping(value = "/hello.html")
public String testMVC(Model model) {
model.addAttribute("message", "Anshul !!");
return "hello";
}
}
helloworld-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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
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-3.2.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.spring.mvc.controllers" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
hello.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
The issue got resolved.
I think the issue was with my system or eclipse itself.
Ran the code on other machine and it worked just fine.
Thanks for replying #alex.b
Check with your directory
make sure that you have build path for required jar files
if it is correct it should work

Spring MVC - When I am click on Submit button on jsp (Post request) my URL changed and my work flow not coming into controller class

When I am click on Submit button on jsp (Post request) my URL changed and my work flow not coming into controller class
First time when I hit URL http://localhost:9080/LaunchingGateway_Maintance/services/Test
my jsp will be opend but when I click on Vendor button on jsp URL will changed to http://localhost:9080/validate and my flow not comming to my controller and on browser I got The webpage cannot be found
please help me I am stuck here from last two week.
my Controller class is : -
package com.us.launchingGatewayController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
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;
import com.us.bean.SSOConfigDynaFldsSrveLstBean;
import com.us.validator.SSOconfigValidator;
#Controller
public class LaunchingGatewayController {
#Autowired
SSOConfigDynaFldsSrveLstBean SSOConfigDynaFldsSrveLstBean;
#Autowired
ModelAndView modelNView;
#Autowired
SSOconfigValidator SSOConfigValidator;
#RequestMapping(value = "/Test", method = RequestMethod.GET)
public ModelAndView test(HttpServletRequest httpRequest, HttpServletResponse resp)throws Exception {
System.out.println("Test In test");
modelNView.setViewName("test");
return modelNView;
}
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(){
System.out.println("hi");
return "test";
}
#RequestMapping(value = "/validate", method = RequestMethod.POST)
public ModelAndView validate(HttpServletRequest httpRequest, HttpServletResponse resp) throws Exception{
System.out.println("Test In Validate");
SSOConfigValidator.validateSSOconfig(SSOConfigDynaFldsSrveLstBean);
modelNView.setViewName("test");
return modelNView;
}
}
my jsp is :-
<%#page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Launching Gateway Maintenance</title>
<script language="JavaScript" type="text/javascript">
</script>
</head>
<body bgcolor='#E8E8E8'>
<h1 align="center">Launching Gateway Maintenance</h1>
<form method="POST" action="/validate">
<center>
<input type="submit" name="validate" value="Vendor" />
</center>
</form>
</body>
</html>
my web.xml is :-
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>LaunchingGateway_Maintance</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
my spring-dispatcher-servlet.xml is :-
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<context:component-scan base-package="com.us.launchingGatewayController" />
<bean id="ssoConfigValidator" class="com.us.validator.SSOconfigValidator"></bean>
<bean id="modelNView" class="org.springframework.web.servlet.ModelAndView"></bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="../jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Something wrong with you context path. According to form you submitting, try this:
<FORM ACTION="${pageContext.request.contextPath}/validate">
Compare this two urls :
http://localhost:9080/LaunchingGateway_Maintance/services/Test
and
http://localhost:9080/validate
So looks like you are missing some context path LaunchingGateway_Maintance
Check this post as well

When Session time-out occurred, preRenderView of previous screen is executed

When forward method in the filter executed,
preRenderView of previous screen is executed if a screen transfer in the session time-out state.
ex)Flow of processing
Session time-out: Login -> Start(session time-out) -> Error
(Default:Login -> Start -> End)
When Start transition to Error, preRenderView of Start is executed.
Isn't preRenderView of Error called in usual?
I think the SpringBeanFacesELResolver class is the cause.
Is it right to do such behavior?
Please tell me the cause and a plan to avoid.
It's being checked in the following state (excerpt).
Spring 3.2.5-RELEASE
JDK 1.7.0_76
JSF 2.1
WebLogic 12.1.3
LoginAction.java
#org.springframework.stereotype.Component
#org.springframework.context.annotation.Scope("request")
public class LoginAction implements java.io.Serializable {
public LoginAction() {
}
public String login() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
// The STATUS attribute is set in a session.
session.setAttribute("STATUS", "LOGIN");
return "/Sample/start.xhtml";
}
}
StartAction.java
#org.springframework.stereotype.Component
#org.springframework.context.annotation.Scope("request")
public class StartAction implements java.io.Serializable {
public StartAction() {
}
public void preRenderView(javax.faces.event.ComponentSystemEvent event)
throws javax.faces.event.AbortProcessingException {
// Some process
}
public void preRenderViewOnSystemError(javax.faces.event.ComponentSystemEvent event)
throws AbortProcessingException, IOException {
// Some process
}
public String toEnd() {
return "toEnd";
}
}
TestFilter.java
public class TestFilter implements Filter {
#Override
public void init(FilterConfig config) throws ServletException {
}
#Override
public void destroy() {
}
#Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if(request instanceof HttpServletRequest) {
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpSession session = httpRequest.getSession();
// When the STATUS attribute can't get a session, a forward is executed.
String status = (String)session.getAttribute("STATUS");
if (status == null || "".equals(status)) {
RequestDispatcher rd = request.getRequestDispatcher("/faces/Sample/error.xhtml");
rd.forward(request, response);
}
}
chain.doFilter(request, response);
}
}
start.xhtml
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>Sample Start</title>
</h:head>
<h:body>
<f:event listener="#{startAction.preRenderView}" type="preRenderView"/>
<h:form action="" id="form1" method="post" enctype="multipart/form-data">
<h:commandButton action="#{startAction.toEnd}" id="toEnd" type="submit" value="End"></h:commandButton>
::
</h:form>
</h:body>
</html>
error.xhtml
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>Sample Error</title>
</h:head>
<h:body>
<f:event listener="#{startAction.preRenderViewOnSystemError}" type="preRenderView"/>
<h:form action="" id="form1" method="post">
Error
</h:form>
</h:body>
</html>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/componentScanSettings.xml
/WEB-INF/datasource-context.xml
</param-value>
</context-param>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/Sample/Sample-faces-config.xml, /WEB-INF/faces-config.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<filter>
<filter-name>multipartFilter</filter-name>
<filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>multipartFilter</filter-name>
<url-pattern>/faces/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>Test</filter-name>
<filter-class>com.xxx.filter.TestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Test</filter-name>
<url-pattern>/faces/Sample/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- The expiration date of a session is made short. -->
<session-config>
<session-timeout>1</session-timeout>
</session-config>
Sample-faces-config.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<navigation-rule>
<description>Start</description>
<display-name>Start</display-name>
<from-view-id>/Sample/start.xhtml</from-view-id>
<navigation-case>
<from-outcome>toEnd</from-outcome>
<to-view-id>/Sample/end.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
faces-config.xml
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
componentScanSettings.xml
<?xml version="1.0" encoding="UTF-8"?>
<bean
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'
xmlns:context='http://www.springframework.org/schema/context'
xmlns='http://www.springframework.org/schema/beans'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<context:annotation-config />
<context:component-scan base-package='com.xxx' />
</bean>
datasource-context.xml
<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<tx:annotation-driven proxy-target-class="true" order="100" />
<bean id="targetDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<property name="targetDataSource" ref="targetDataSource" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="filterMultipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>100000</value>
</property>
<property name="maxInMemorySize">
<value>10240</value>
</property>
</bean>
</beans>

MVC using Spring hello world example

I am trying to implement the hello world example using Spring MVC but it is not giving the desired result.
This is my jsp page
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Print: ${message} world</h2>
</body>
</html>
HelloController.java
package com.sbv.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
#RequestMapping("/hello")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model)
{
model.addAttribute("message", "Hello");
return "index";
}
}
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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>LoginSpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
HelloServlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.sbv.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
The output which I am getting is
Print: world
The ${message} is not getting printed
Someone please help me with this
Thanks in advance
The message attribute is added to the MODEL in the printHello method:
#Controller
#RequestMapping("/hello")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model)
{
model.addAttribute("message", "Hello");
return "index";
}
}
which will get executed when you make a GET request for /hello (http://host:port/appContext/hello). But you have the below entry in web.xml file:
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
and the web container will use this file for appending to a request for / to show index.jsp to the user, as you don't have any handler method mapped to / URL. So, in order to show the message to the user, remove the <welcome-file-list> entry from web.xml and change HelloController to this:
#Controller
#RequestMapping("/")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model)
{
model.addAttribute("message", "Hello");
return "index";
}
}
Get rid of
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
And make your request to
http://yourhost:yourport/YourApp/hello
to match your #Controller mapping.
If you make your request to
http://yourhost:yourport/YourApp
and any of the <welcome-file> entries exist, those will be chosen before your Servlet is hit.

Resources