在我的Spring MVC项目中,我无法使用scope = session访问bean的参数.Itto Http会话中有一个名为“scopedTarget.user”的bean。我想在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>
答案 0 :(得分:0)
如果将这些anotations @Component
和@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
添加到要在会话中管理的POJO中,则可以使用您在问题中提及的方法在JSP中使用它(和在您的控制器中自动装配它)。确保两件事:
不要覆盖toString(),否则将是分配给${sessionScope['scopedTarget.user']
的结果。
你的POJO,在这种情况下是用户,需要被Spring的组件扫描器“抓住”