В моей MVC-проекте Spring я не могу получить доступ к параметрам bean с scope = session.Into Http session есть bean с именем "scopedTarget.user". Я хочу print, на странице jsp, имя пользователя. Почему так трудно получить доступ к этим параметрам? Где я ошибаюсь?Spring: bean with scope = session in jsp
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>
конфиг-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>
намерены из 'session' область действия боб отличается, то необходимо просто использовать в странице JSP , Он предназначен для инъекции в другие бобы. Если вы хотите выставить пользователя, просто используйте атрибуты сеанса для себя. –
Ваше имя bean-файла кажется «пользователем», а не «scopedTarget.user». Вместо этого используйте $ {user.nome} в JSP. – Angad
Я пробовал оба с $ {scopedTarget.user} и с $ {user.nome}, но я не могу получить доступ к информации bean's – Alex