Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring:jsp中scope=session的bean_Spring_Jsp_Spring Mvc - Fatal编程技术网

Spring:jsp中scope=session的bean

Spring:jsp中scope=session的bean,spring,jsp,spring-mvc,Spring,Jsp,Spring Mvc,在我的Spring MVC项目中,我无法访问scope=session的bean参数。在Http会话中,有一个名为“scopedTarget.user”的bean。我想在jsp页面中打印用户名。为什么访问此参数如此困难?我错在哪里 控制室: @Controller public class ControllerHome { @Autowired private User user; @RequestMapping(value="/",method=RequestMethod.GET) pub

在我的Spring MVC项目中,我无法访问scope=session的bean参数。在Http会话中,有一个名为“scopedTarget.user”的bean。我想在jsp页面中打印用户名。为什么访问此参数如此困难?我错在哪里

控制室:

@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:/";
}

}
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>

配置
org.springframework.web.servlet.DispatcherServlet
1.
配置
/
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>

如果将这些变量
@Component
@Scope(value=“session”,proxyMode=ScopedProxyMode.TARGET_CLASS)
添加到要在会话中管理的POJO中,则可以使用您在问题中提到的方法在JSP中使用它(并在控制器中自动连接)。但要确保有两件事:

  • 不要重写toString(),否则将把该结果分配给
    ${sessionScope['scopedTarget.user']

  • 您的POJO(在本例中为用户)需要被Spring的组件扫描器“捕获”


  • 会话
    作用域bean的意图不同于仅在jsp页面中使用。它旨在注入到其他bean中。如果您想公开用户,只需自己使用会话属性即可。您的bean名称似乎是“user”而不是“scopedTarget.user”。在jsp中使用${user.nome}。我用${scopedTarget.user}和${user.nome}但是我无法访问bean的信息。为什么在${sessionScope['scopedTarget.user'].nome}中使用“sesionScope”。您只使用了${user.nome}吗?您还可以将config-servlet.xml中的aop配置更改为
    <?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>
    
    <?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>