简体   繁体   English

在我的Spring 3.2 MVC应用程序中,为什么登录后我的JSP EL表达式不会被评估?

[英]In my Spring 3.2 MVC application, why aren't my JSP EL expressions being evaluated after login?

I want to implement spring3.2.0 web mvc following the example: http://programmersplanet.wordpress.com/2011/11/26/4/ 我想按照示例实现spring3.2.0 web mvc: http ://programmersplanet.wordpress.com/2011/11/26/4/

but I fail. 但我失败了。 Finally I get the result which is: Successfully logged in: ${user.username} , you see the variable is useless(JSP doesn't work). 最后我得到的结果是: Successfully logged in: ${user.username} ,你看到变量没用(JSP不起作用)。 I add some output to debug, it proves @Controller, @RequestMapping work. 我添加一些输出到debug,它证明了@Controller,@ RequestMapping工作。 So I don't know what is wrong with my coding. 所以我不知道我的编码有什么问题。

spring-servlet.xml 为spring-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"
       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
         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:annotation-config/>
    <mvc:annotation-driven/>
    <context:component-scan base-package="demo.spring"/>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

LoginController.java LoginController.java

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/")
public class LoginController {
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView displayLoginView() {
        UserForm userForm = new UserForm();
        ModelAndView view = new ModelAndView("login");
        view.addObject(userForm);
        return view;
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView doLogin(@ModelAttribute("user") UserForm user, BindingResult bindingResult, SessionStatus sessionStatus) {
        ModelAndView model = new ModelAndView("home");
        System.out.println(user.getUsername());// this line is right.
        return model;
    }
}

home.jsp 针对home.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!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>home</title>
</head>
<body>
Successfully logged in: ${user.username}
</body>
</html>

web.xml web.xml中

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>DispatchServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatchServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app

The probable cause of your issue is you have added "userForm" in your Controller and trying to access it as ${user.username} in JSP ! 您的问题的可能原因是您在Controller中添加了“userForm”并尝试在JSP中以$ {user.username}的形式访问它!

Also check if you have disabled EL expressing in your web.xml. 还要检查您是否在web.xml中禁用了EL表达式。 We have 1000's of user who are relying on Spring MVC, which is the most reliable Java EE framework till date. 我们有1000名用户依赖Spring MVC,这是迄今为止最可靠的Java EE框架。

my web.xml is generated by maven automatically. 我的web.xml是由maven自动生成的。 it is wrong. 这是错误的。

Before modification(by default): 修改前(默认情况下):

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >

After modification: 修改后:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM