3

例に従って spring3.2.0 Web MVC を実装したい: http://programmersplanet.wordpress.com/2011/11/26/4/

しかし、私は失敗します。最後に、次の結果が得られSuccessfully logged in: ${user.username}ます。変数が役に立たないことがわかります(JSPは機能しません)。デバッグする出力を追加すると、@Controller、@RequestMapping が機能することが証明されます。したがって、コーディングの何が問題なのかわかりません。

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

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

<%@ 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

<!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
4

2 に答える 2

2

問題の考えられる原因は、コントローラーに「userForm」を追加し、JSP で ${user.username} としてアクセスしようとしたことです。

また、web.xml で EL 表現を無効にしているかどうかも確認してください。これまでで最も信頼性の高い Java EE フレームワークである Spring MVC に依存している何千人ものユーザーがいます。

于 2012-12-19T06:00:00.897 に答える
0

私のweb.xmlはmavenによって自動的に生成されます。違います。

変更前 (デフォルト):

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

変更後:

<?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"> 
于 2012-12-19T06:16:37.660 に答える