0

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

过去两天我尝试的人只是简单地在我的控制器中的请求方法之间传递属性,尝试了很多不同的方式,但没有任何反应。我有 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>
4

2 回答 2

0

弗吉尼亚州,

尝试这个:

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

网页.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> 

创建日期.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("unused") // private static final 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> 
于 2013-06-18T00:18:51.770 回答
0

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

  • 你有日期解析器吗?您的表单 bean 上有一个 Date 类型的属性,除非您告诉它,否则 Spring 不会自动知道日期格式。在 Spring 3.2.3 上,您可以简单地使用@DateTimeFormat注释
  • 您是否正确设置了视图解析器?我假设“index”和“add”解析为 index.jsp 和 add.jsp,确保你没有编辑错误的文件
  • 你有没有弄乱你模型中的东西的Spring MVC 处理程序拦截器/servlet 过滤器?
于 2013-06-17T23:33:49.950 回答