【问题标题】:Simple, passing attributes Spring MVC misconception简单,传递属性 Spring MVC 误解
【发布时间】:2013-06-17 17:31:19
【问题描述】:

大家好,感谢您帮助世界各地的许多人。

过去两天我尝试的伙计们只是简单地在我的控制器中的请求方法之间传递属性,尝试了很多不同的方式,但没有任何反应。我有 bean CreationDate,我需要填写该 bean 中的表单属性,然后在我的第二页上简单地呈现它们。我在浏览器的 url 栏中看到它正在传递(因为我使用 GET 方法来传递)但它没有出现在第二页上,只是空白列表。

我的控制器类:

@Controller
public class HomeController{


    private static final long serialVersionUID = 4825408935018763217L;

    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);  

    @Autowired
    private ControllerSupportClass controllerSupportClass; 


        public void setControllerSupportClass(
                ControllerSupportClass controllerSupportClass) {
            this.controllerSupportClass = controllerSupportClass;
        }




        @RequestMapping(value ="/index", method=RequestMethod.GET)
        public String index(Model model) {
            CreationDate creationDate = new CreationDate();
            model.addAttribute("creationD", creationDate);
            return "index";
        }



        @RequestMapping(value="/add", method=RequestMethod.GET)
        public String addingData(@ModelAttribute("creationD") CreationDate creationDate, BindingResult result, Model model) {

            model.addAttribute("creationD", creationDate);

            return "add";

        }

}

我的豆子:

public class CreationDate implements Serializable {


    private static final long serialVersionUID = 1648102358397071136L;


    private int dateId;

        @Id
        @GeneratedValue(strategy=IDENTITY)
        @Column(name="DATE_ID")
        public int getDateId() {
            return dateId;
        }

        public void setDateId(int dateId) {
            this.dateId = dateId;
        }


    private Date particularDate;

        @Column(name="PARTICULAR_DATE")
        public Date getParticularDate() {
            return particularDate;
        }

        public void setParticularDate(Date particularDate) {
            this.particularDate = particularDate;
        }


    private int version;

        @Version
        @Column(name="VERSION")
        public int getVersion() {
            return version;
        }

        public void setVersion(int version) {
            this.version = version;
        }


    private Date childGoSchoolDate;

        @Temporal(TemporalType.DATE)
        @Column(name="CHILD_GO_SCHOOL_DATE")
        public Date getChildGoSchoolDate() {
            return childGoSchoolDate;
        }

        public void setChildGoSchoolDate(Date childGoSchoolDate) {
            this.childGoSchoolDate = childGoSchoolDate;
        }


    private Date childAdmissionDate;

        @Temporal(TemporalType.DATE)
        @Column(name="CHILD_ADMISSION_DATE")
        public Date getChildAdmissionDate() {
            return childAdmissionDate;
        }

        public void setChildAdmissionDate(Date childAdmissionDate) {
            this.childAdmissionDate = childAdmissionDate;
        }   

}

我的表单页面:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%>
<%@ 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=utf-8">
<title>Страница выборки</title>
</head>

<body>

<h3>Вставка данных:</h3>

<form:form modelAttribute="creationD" method="GET" action="add">


<form:label path="particularDate">Particular Date</form:label>
<form:input path="particularDate" /> <br>

<form:label path="childGoSchoolDate">Child go to School</form:label>
<form:input path="childGoSchoolDate"/> <br>


<form:label path="childAdmissionDate">Child admission Date</form:label>
<form:input path="childAdmissionDate"/> <br>


<input type="submit" value="Save"/>

</form:form>

</body>

</html>

我需要从表单中租用数据的第二个页面:

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>    
<%@ 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 XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>

<h1>Result:</h1>

Attribute 1:<c:out value="${creationD.particularDate}"/>
Attribute 2:<c:out value="${creationD.childGoSchoolDate}"/>
Attribute 3:<c:out value="${creationD.childAdmissionDate}"/>

</body>
</html>

【问题讨论】:

    标签: spring jsp model-view-controller spring-mvc


    【解决方案1】:

    我会检查以下内容以隔离问题:

    • 您有日期解析器吗?您的表单 bean 上有一个 Date 类型的属性,除非您告诉它,否则 Spring 不会自动知道日期格式。在 Spring 3.2.3 上,您可以简单地 use @DateTimeFormat annotation
    • 您是否正确设置了视图解析器?我假设“index”和“add”解析为 index.jsp 和 add.jsp,请确保您没有编辑错误的文件
    • 您是否有任何Spring MVC handler interceptor / servlet 过滤器会弄乱您模型中的内容?

    【讨论】:

    • 是的,谢谢格瑞坦。您提到的所有内容都非常重要。我可能会更好地理解所有这些以解决问题。确实,我对这些主题了解得不够好,但我应该。谢谢。
    • 您知道问题出在@DateTimeFormat 中。感谢您的帮助。
    【解决方案2】:

    v.a,

    试试这个:

    注意:我注释掉了控制器中的日志记录以节省时间。您可以将其重新添加。 我还从您的控制器中取出了数据库注释,再次以节省时间,并且我还将您在 /add 例程中构建属性的方式更改为我熟悉的样式。 最后但并非最不重要的一点是,我还稍微改变了您显示输出的方式..

    web.xml:

    <web-app id="WebApp_ID" version="2.4" 
      xmlns="http://java.sun.com/xml/ns/j2ee"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
    
      <display-name>Spring MVC Form Handling</display-name> 
    
      <servlet> 
          <servlet-name>Student</servlet-name> 
          <servlet-class> 
             org.springframework.web.servlet.DispatcherServlet 
          </servlet-class> 
          <load-on-startup>1</load-on-startup> 
      </servlet> 
    
      <servlet-mapping> 
          <servlet-name>Student</servlet-name> 
          <url-pattern>/</url-pattern> 
      </servlet-mapping> 
    
    </web-app> 
    

    Student-servlet.xml‏:

    <?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
    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"> 
    
    <context:component-scan base-package="com.hcsc" /> 
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
       <property name="prefix" value="/WEB-INF/jsp/" /> 
       <property name="suffix" value=".jsp" /> 
    </bean> 
    
    </beans> 
    

    CreationDate.Java:

    package com.hcsc; 
    
    import java.util.Date; 
    
    public class CreationDate { 
    
    private int dateId; 
    
        public int getDateId() { 
            return dateId; 
        } 
    
        public void setDateId(int dateId) { 
            this.dateId = dateId; 
        } 
    
    
      private Date particularDate; 
        public Date getParticularDate() { 
            return particularDate; 
        } 
    
     public void setParticularDate(Date particularDate) { 
            this.particularDate = particularDate; 
        } 
    
    
     private int version; 
        public int getVersion() { 
            return version; 
        } 
    
     public void setVersion(int version) { 
            this.version = version; 
        } 
    
    
     private Date childGoSchoolDate; 
        public Date getChildGoSchoolDate() { 
            return childGoSchoolDate; 
        } 
    
     public void setChildGoSchoolDate(Date childGoSchoolDate) { 
            this.childGoSchoolDate = childGoSchoolDate; 
        } 
    
    
     private Date childAdmissionDate; 
        public Date getChildAdmissionDate() { 
            return childAdmissionDate; 
        } 
    
     public void setChildAdmissionDate(Date childAdmissionDate) { 
            this.childAdmissionDate = childAdmissionDate; 
        }    
    } 
    

    HomeController.java:

    package com.hcsc; 
    
      import org.springframework.stereotype.Controller; 
      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.servlet.ModelAndView; 
      import org.springframework.ui.ModelMap; 
    
    @Controller 
    public class HomeController{ 
    
    
        private static final long serialVersionUID = 4825408935018763217L; 
    

    // @SuppressWarnings("未使用") // 私有静态最终 Logger logger = LoggerFactory.getLogger(HomeController.class);

          @RequestMapping(value ="/index", method=RequestMethod.GET) 
          public ModelAndView index() { 
            return new ModelAndView("index","command", new CreationDate()); 
        } 
    
        @RequestMapping(value="/add", method=RequestMethod.GET) 
        public String addingData(@ModelAttribute("creationD")CreationDate creationDate, ModelMap   model) { 
    
                model.addAttribute("particularDate", creationDate.getParticularDate()); 
                model.addAttribute("childGoSchoolDate", creationDate.getChildGoSchoolDate()); 
                model.addAttribute("childAdmissionDate", creationDate.getChildAdmissionDate()); 
    
            return "add"; 
    
        } 
    }
    

    index.jsp:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%> 
    <%@ 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=utf-8"> 
    <title>Страница выборки</title> 
    </head> 
    
    <body> 
    
    <h3>Вставка данных:</h3> 
    
    <form:form method="GET" action="/Student/add"> 
    
    
    <form:label path="particularDate">Particular Date</form:label> 
    <form:input path="particularDate" /> <br> 
    
    <form:label path="childGoSchoolDate">Child go to School</form:label> 
    <form:input path="childGoSchoolDate"/> <br> 
    
    
    <form:label path="childAdmissionDate">Child admission Date</form:label> 
    <form:input path="childAdmissionDate"/> <br> 
    
    
    <input type="submit" value="Save"/> 
    
    </form:form> 
    
    </body> 
    
    </html> 
    

    添加.jsp:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0   Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <title>Student UI Output</title> 
    </head> 
    <body> 
    
    <h1>Result:</h1> 
    
    Attribute 1: <c:out value="${particularDate} "/><br /> 
    Attribute 2: <c:out value="${childGoSchoolDate} "/><br /> 
    Attribute 3: <c:out value="${childAdmissionDate} "/><br /> 
    
    </body> 
    </html> 
    

    【讨论】:

    • 是的,非常感谢 wSchmidt,它确实有效。你能告诉我我的问题是什么,我做错了什么吗?在控制器内部的方法之间传递属性的机制是什么?感谢您的帮助
    猜你喜欢
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 2012-03-18
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多