2011-09-15 3 views
1

스프링 MVC 테스트 응용 프로그램을 작동 시키려고했습니다. 여기 내 모든 봄 코드가있다.action = "somename"은 <MVC annotation에서 작동하지 않습니다>

내 JSP home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<%@ page session="false" %> 
<%@ include file="/WEB-INF/views/header.jsp" %> 

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html"> 
<title>iBank - Home-version 2.0</title> 
</head> 
<body> 
<h1 align="center">Welcome to iBank-Dhiren</h1> 
<h2 align="center">Your Online Bank Portal</h2> 
<p align="center"> 
Today is ${today}.<br/> 
<a href="<%=request.getContextPath()%>/admin.htm">Modified Administration Site-version-1.0 </a> 
</p> 
</body> 
</html> 

<p> 
    <form:form method="post" action="/user.jsp" modelAttribute="user"> 

     <div> 
      <form:label path="firstName">Name:</form:label> 
      <form:input path="firstName"/> 
      <form:errors path="firstName" /> 
     </div> 
     <div> 
      <form:label path="password">Password:</form:label> 
      <form:input path="password" /> 
      <form:errors path="password" /> 
     </div> 
     <div> 
      <form:label path="middleName">Middle name:</form:label> 
      <form:input path="middleName" /> 
      <form:errors path="middleName" /> 
     </div> 
     <div> 
      <form:label path="lastName">LastName:</form:label> 
      <form:input path="lastName" /> 
      <form:errors path="lastName" /> 
     </div> 


     <div> 
      <input type="submit" value=" OK "/> 
     </div> 
    </form:form> 
</p> 

</html> 

내 UserLoginController

import javax.validation.Valid; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.ui.ModelMap; 
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; 

@Controller 

    //@RequestMapping() 
    public class UserLoginController { 

    public UserLoginController(){ 

    } 
     //@RequestMapping(value="/get" , method = RequestMethod.GET) 
    //@ModelAttribute("user") 
     public String get(final ModelMap model) { 

      User userForm = new User(); 
     model.addAttribute("userLogin", userForm); 
     return "form"; 
     } 

     @RequestMapping(value="/user.jsp", method = RequestMethod.POST) 
     public String post(final User user, final BindingResult result, Model mv) { 

      String test=" inside here"; 

      return "success"; 
     } 
    } 

사용자 클래스

import javax.validation.constraints.Size; 

import org.hibernate.validator.constraints.NotEmpty; 

public class User { 
/* @NotEmpty 
    @Size(max = 20) 
    */ 
private String userId; 
    /*@NotEmpty 
    @Size(max = 20) 
*/ 
private String password; 
private String firstName; 
private String middleName; 
private String lastName; 
//private int userAccessLevel; 
/** 
* @return the userId 
*/ 
public String getUserId() { 
    return userId; 
} 
/** 
* @param userId the userId to set 
*/ 
public void setUserId(String userId) { 
    this.userId = userId; 
} 
/** 
* @return the password 
*/ 
public String getPassword() { 
    return password; 
} 
/** 
* @param password the password to set 
*/ 
public void setPassword(String password) { 
    this.password = password; 
} 
/** 
* @return the firstName 
*/ 
public String getFirstName() { 
    return firstName; 
} 
/** 
* @param firstName the firstName to set 
*/ 
public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 
/** 
* @return the middleName 
*/ 
public String getMiddleName() { 
    return middleName; 
} 
/** 
* @param middleName the middleName to set 
*/ 
public void setMiddleName(String middleName) { 
    this.middleName = middleName; 
} 
/** 
* @return the lastName 
*/ 
public String getLastName() { 
    return lastName; 
} 
/** 
* @param lastName the lastName to set 
*/ 
public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 
/** 
* @return the userAccessLevel 
* 
public int getUserAccessLevel() { 
    return userAccessLevel; 
} 
*/ 
/** 
* @param userAccessLevel the userAccessLevel to set 
* 
public void setUserAccessLevel(int userAccessLevel) { 
    this.userAccessLevel = userAccessLevel; 
}*/ 

} 

이러한이의 context.xml 파일을 서블릿-context.xml에 있습니다

나는 웹의 첫 페이지로 이동하려고 할 때 home.jsp이 모든으로 오류를

web.xml을

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <display-name>appServlet</display-name> 
    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
    <param-name>log4jConfigLocation</param-name> 
    <param-value>/WEB-INF/log4j.xml</param-value> 
</context-param> 
<listener> 
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
</listener> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/appServlet/root-context.xml</param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/</url-pattern> 

    </servlet-mapping> 
</web-app> 

을 오지 않는 이유

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

     <!-- Imports user-defined @Controller beans that process client requests --> 

    <context:component-scan base-package="mytest.apps" /> 

</beans:beans> 

(210)는 어떤 하나 말해 줄 수 application Tomcat에서이 오류가 발생합니다.

INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned where no TLDs were found. Skipping JAR scanning can improve startup time and JSP compilation time. 
log4j:ERROR Attempted to append to closed appender named [console]. 
Sep 14, 2011 10:02:39 PM org.apache.catalina.core.ApplicationDispatcher invoke 
SEVERE: Servlet.service() for servlet jsp threw exception 
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute 
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141) 
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174) 
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194) 
    at org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:129) 
    at org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119) 
    at org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89) 
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102) 
. 
. 
Sep 14, 2011 10:02:39 PM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/AdministrativeApplication] threw exception [An exception occurred processing JSP page /WEB-INF/views/home.jsp at line 25 

22:  <form:form method="post" action="/user.jsp" modelAttribute="user"> 
23:  
24:   <div> 
25:    <form:label path="firstName">Name:</form:label> 
26:    <form:input path="firstName"/> 
27:    <form:errors path="firstName" /> 
28:   </div> 


Stacktrace:] with root cause 
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute 
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141) 

당신은 "사용자"의 ModelAttribute 모 데르와 양식을 사용하지만, "위해 userLogin"등의 ModelAttribute 모 데르를 가하고 있습니다

감사 디렌

+0

우리가 살펴볼 많은 코드가 있습니다. 문제를 현지화 할 수 있습니까? –

+0

이 코드는 작동하지 않습니다. 나는

이유를 이해할 수 없다. JSP로 부분을 주석 처리하면 이름 : JSP가 보여줍니다 쪽으로. – djoshi

+0

마침내 해결책을 찾아 냈습니다. 자세한 설명을 해줄 것입니다. 필자의 의견으로는 구현해야 할 메소드의 서명입니다. – djoshi

답변

1

주요 문제가 발생했습니다. BindingResult 및 Model 및 특성이 있지만 서명 순서가 올바르지 않습니다. 스프링은 이것을 참조 링크 당 오류로 재확인한다. http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html 예 15.1. 나에게 BindingResult의 문제를 발생 BindingResult와 @ModelAttribute

내가 서명의 올바른 순서를 가지고 있지되었다
@RequestMapping(method = RequestMethod.POST) 
public String processSubmit(@ModelAttribute("pet") Pet pet, 
    Model model, BindingResult result) { … } 

Note, that there is a Model parameter in between Pet and BindingResult. To get this working you have to reorder the parameters as follows: 

@RequestMapping(method = RequestMethod.POST) 
public String processSubmit(@ModelAttribute("pet") Pet pet, 
    BindingResult result, Model model) { … } 

의 잘못된 순서는 인식되지 않습니다.

1

도와주세요. 둘 중 하나를 변경하여 일관성을 유지하면 효과가 있습니다.

+0

이 코드는 UserLoginController에 있습니다. public String get (최종 ModelMap 모델) { 사용자 userForm = 새 사용자(); model.addAttribute ("userLogin", userForm); return "form"; } 이것은 모델 속성을 호출하는 것과 어떤 관련이 있습니까? 감사합니다 – djoshi

+0

봄이 양식 태그를 만들려고합니다. 당신은 그 데이터를 얻을 수있는 user라는 이름의 모델 객체가 있다고 말했지만, 태그를 렌더링하려고 시도 할 때 데이터는 존재하지 않습니다. userLogin이라는 모델 객체가 무엇입니까? 첫 번째 페이지를로드 할 수 없으므로 게시물 코드가 호출되지 않습니다.이 라인을 변경하면 model.addAttribute ("userLogin", userForm); to addAttribute ("user", userForm); 작동해야합니다. – dlawrence

+0

그건 여전히 같은 오류가 발생하는 데 도움이되지 못했습니다. – djoshi

관련 문제