0

spring を使用して UI で日付の問題が発生しました。データベースからクエリを取得すると、データ形式が UI に表示さ1987-02-12 00:00:00.0れます。値を送信すると、null がデータベースに入ります。私は使用しました

return getJdbcTemplate().update( QUERY_CREATE_PROJECT, new Object[] { employeeProject.getEmployeeNumber(), employeeProject.getProjectCode(), employeeProject.getStartDate(), employeeProject.getEndDate(), employeeProject.getProjectRole() }) != 0 ? true : false; }春の方法、私の要件は、日付を形式(dd / MM / yyyy)で表示し、同じ形式で日付を挿入することです。カスタム日付で日付の形式を変換する方法plzは私を助け、カスタマイズの使用場所を教えてくれます日付、コントローラー層、DAO層、またはサービス層で日付をカスタマイズする必要があります

私のコントローラーの作成方法は

  package com.nousinfo.tutorial.controllers;
    import java.util.Map;
    import javax.validation.Valid;
    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.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    import com.nousinfo.tutorial.model.ProjectForm;
    import com.nousinfo.tutorial.service.impl.ProjectServiceImpl;
    import com.nousinfo.tutorial.service.model.EmployeeProjectBO;

    /**
     * 
     * @author ankurj
     * 
     */
    @Controller
    @RequestMapping("projectController")
    public class ProjectController {
        private ProjectServiceImpl projectServiceImpl;

        public ProjectServiceImpl getProjectServiceImpl() {
            return projectServiceImpl;
        }

        public void setProjectServiceImpl(ProjectServiceImpl projectServiceImpl) {
            this.projectServiceImpl = projectServiceImpl;
        }

        /**
         * Used to set the view 
         * @param id
         * @return
         * @throws Exception
         */
        @RequestMapping(value = "/projectForm", method = RequestMethod.GET)
        public ModelAndView view(@RequestParam("id") int id) throws Exception {
            ModelAndView modelAndView = new ModelAndView();
            System.out.println(id);
            ProjectForm projectForm = new ProjectForm();
            projectForm.setEmployeeNumber(id);

            modelAndView.addObject("projectForm", projectForm);
            modelAndView.setViewName("projectForm");
            return modelAndView;
        }

        /**
         * Create the project for an employee
         * @param projectForm
         * @param bindingResult
         * @param model
         * @return
         * @throws Exception
         */
        @RequestMapping(value = "/createProject", method = RequestMethod.POST)
        public String createEmployee(@Valid ProjectForm projectForm,
                BindingResult bindingResult, Map<String, ProjectForm> model)
                throws Exception {
            String form = null;
            if (bindingResult.hasErrors()) {
                return "projectForm";
            }

            model.put("projectForm", projectForm);
            projectForm.setUpdateStatus("A");
            if (projectForm.getUpdateStatus().charAt(0) == 'A') {
                boolean flag = projectServiceImpl
                        .actionDecider(convertprojectFormToprojectBO(projectForm));
                if (flag == false)
                    form = "DBError";
                else
                    form = "Success";

            }
            return form;
        }
        /**
     * This method update the existing  detail of project
     * @param projectForm
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/updateProject", method = RequestMethod.POST)
    public String updateDepartment(
            @ModelAttribute("projectForm") ProjectForm projectForm,
            BindingResult bindingResult) {

        String form = null;
        projectForm.setUpdateStatus("M");
        if (projectForm.getUpdateStatus().charAt(0) == 'M') {
            boolean flag = projectServiceImpl
                    .actionDecider(convertprojectFormToprojectBO(projectForm));
            if (flag == false)
                form = "DBError";
            else
                form = "Success";

        }

        return form;
    }

これが私のモデルクラスです

package com.nousinfo.tutorial.model;
import java.util.Date;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.NumberFormat;

public class ProjectForm {
    @NotNull
    @NumberFormat
    @Min(1)
    private Integer employeeNumber;
    @NotEmpty(message = "project code can't be blank")
    private String projectCode;
    private Date startDate;
    private Date endDate;
    private String role;
    private String updateStatus;

    public String getProjectCode() {
        return projectCode;
    }

    public void setProjectCode(String projectCode) {
        this.projectCode = projectCode;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public Integer getEmployeeNumber() {
        return employeeNumber;
    }

    public void setEmployeeNumber(Integer employeeNumber) {
        this.employeeNumber = employeeNumber;
    }

    public String getUpdateStatus() {
        return updateStatus;
    }

    public void setUpdateStatus(String updateStatus) {
        this.updateStatus = updateStatus;
    }

}

これは私の日付用のjspです

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<!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=ISO-8859-1">
<title></title>
<script>
    function actionChange(url) {

        if (url == 'Save') {
            document.form.action = "/EmployeeWebSpring/projectController/updateProject";

        }
        if (url == 'Delete') {
            document.form.action = "/EmployeeWebSpring/projectController/deleteProject";
        }

    }

    function home() {
        window.location.href = "/EmployeeWebSpring/search/searchspring";

    }
</script>
</head>
<body background="../images/flower.jpg">
<img src="../images/Header.png" width="1500"/>
<hr width="1500">
    <form:form name='form' commandName="projectForm">
        <fmt:message key="project.searchResult.header" />
        <c:choose>
            <c:when test="${empty requestScope.projectBO}">
                <fmt:message key="searchResult.noresult" />
            </c:when>
            <c:otherwise>
                <table align="center">

                    <form:hidden path="updateStatus" />
                    <tr align="center">
                        <th><fmt:message key="employeeNumber" /></th>
                        <td><form:input path="employeeNumber"
                                value="${requestScope.projectBO.employeeNumber}" /></td>
                    </tr>

                    <tr align="center">
                        <th><fmt:message key="projectCode" /></th>
                        <td><form:input path="projectCode"
                                value="${requestScope.projectBO.projectCode}" /></td>
                    </tr>
                    <tr>
                    <tr align="center">
                        <th><fmt:message key="startDate" /></th>
                        <td><form:input path="startDate"
                                value="${requestScope.projectBO.startDate}" /></td>
                    </tr>
                    <tr>
                    <tr align="center">
                        <th><fmt:message key="endDate" /></th>
                        <td><form:input path="endDate"
                                value="${requestScope.projectBO.endDate}" /></td>
                    </tr>
                    <tr>
                    <tr align="center">
                        <th><fmt:message key="role" /></th>
                        <td><form:input path="role"
                                value="${requestScope.projectBO.role}" /></td>
                    </tr>


                </table>
                <br>
                <center>
                    <table>
                        <tr>

                            <td><input type="submit" name="method" value="Save"
                                onclick="actionChange(this.value)" /></td>

                            <td><input type="submit" name="method" value="Delete"
                                onclick="actionChange(this.value)" /></td>

                            <td><input type="button" onclick="home" value="Cancel" /></td>


                        </tr>
                    </table>
                </center>

            </c:otherwise>
        </c:choose>
        <br />
        <fmt:message key="searchResult.searchAgain" />
        <a href="/EmployeeWebSpring/search/searchspring"> <fmt:message
                key="searchResult.click" />
        </a>


    </form:form>
</body>
</html>
4

1 に答える 1

0

必要なのは、日付タイプのカスタムエディタだけです。これを使用して、着信および発信の日付オブジェクトをフォーマットできます。これについては、投稿ですでに説明されています。

SpringMVC-日付フィールドのバインド

于 2013-01-23T10:33:19.790 に答える