gpt4 book ai didi

java - 在 Spring 中向对象添加日历值

转载 作者:行者123 更新时间:2023-12-01 13:44:46 24 4
gpt4 key购买 nike

我正在尝试构建一个基本的 Spring 应用程序来获取有关教学类(class)的信息并将其添加到 MySQL 数据库中。

显示一个表单,用户输入值并单击“提交”,这会通过 POST 将值发送到我的 Controller 。这些值应存储在 Session 对象中。

这对于所有值都适用,但日期 (start_date, end_date) 和时间 (start_time, end_time) 变量除外,它们保持为 null 并且不会调用它们的 setter。

如何让它将接收到的值存储到所需的日期和时间值中?

这是我的 jsp 文件,包含以下形式:

<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Add a new session | Undergraduate Teaching System</title>

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="<%=request.getContextPath()%>/resources/css/normalize.css">
<link rel="stylesheet" href="<%=request.getContextPath()%>/resources/css/main.css">
<script src="<%=request.getContextPath()%>/resources/js/vendor/modernizr-2.6.2.min.js"></script>
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->

<h1>Add a new teaching session</h1>

<form action="addsessionresult" method="POST">

<!-- Course -->
<label for="course">Course:</label>
<select name="course">
<option>Course 1</option>
<option>Course 2</option>
<option>Course 3</option>
<option>Course 4</option>
<option>Course 5</option>
</select>
<br />
<!-- Sesion name -->
<label for="session_name">Sesion name:</label>
<input type="text" name="session_name" />
<br />
<!-- Date picker -->
<label for="datepicker">Start date:</label>
<input type="text" class="date_picker" name="start_date" />
<br />

<!-- Repeat frequency -->
<label for="session_frequency">Repeat every:</label>
<select name="session_frequency" id="session_frequency">
<option value="0">One-off event</option>
<option value="1">1 week</option>
<option value="2">2 weeks</option>
<option value="3">3 weeks</option>
<option value="4">4 weeks</option>
</select>
<br />
<div id="repeat_until_wrapper" class="hidden">
<label for="end_date">Repeat until:</label>
<input type="text" class="date_picker" name="end_date" />
<br />
</div>

<!-- Time -->
<label for="start_time">Start time:</label>
<select name="start_time">
<option value="09:00">09:00</option>
<option value="10:00">10:00</option>
<option value="11:00">11:00</option>
<option value="12:00">12:00</option>
<option value="13:00">13:00</option>
<option value="14:00">14:00</option>
<option value="15:00">15:00</option>
<option value="16:00">16:00</option>
<option value="17:00">17:00</option>
<option value="18:00">18:00</option>
<option value="19:00">19:00</option>
<option value="20:00">20:00</option>
</select>
<br />
<!-- Duration -->
<label for="session_duration">Duration:</label>
<select name="session_duration">
<option value="15">15m</option>
<option value="30">30m</option>
<option value="45">45m</option>
<option value="60" selected>1h</option>
<option value="75">1h 15m</option>
<option value="90">1h 30m</option>
<option value="105">1h 45m</option>
<option value="120">2h</option>
<option value="135">2h 15m</option>
<option value="150">2h 30m</option>
<option value="165">2h 45m</option>
<option value="180">3h</option>
<option value="195">3h 15m</option>
<option value="210">3h 30m</option>
<option value="225">3h 45m</option>
<option value="240">4h</option>
<option value="255">4h 15m</option>
<option value="270">4h 30m</option>
<option value="285">4h 45m</option>
<option value="300">5h</option>
</select>
<br />
<!-- Lecturer -->
<label for="staff_member">Staff member:</label>
<select name="staff_member">
<option>Stephen Smith</option>
<option>Matthew Brown</option>
<option>Gethin Black</option>
<option>Jeremy Bailey</option>
<option>Joseph Lewis</option>
<option>David Watson</option>
</select>
<br />
<!-- Max attendance -->
<label for="max_attendance">Max. attendance:</label>
<input type="number" name="max_attendance" />
<br />
<!-- Compulsory -->
<label for="compulsory">Is compulsory?</label>
<input type="checkbox" name="compulsory" />
<br />
<!-- Venue -->
<label for="venue">Venue:</label>
<select name="venue">
<option value="Building 513">Building 513</option>
<option value="Building 720">Building 720</option>
<option value="Building 811">Building 811</option>
</select>
<br /><br />
<button type="submit" name="submit_data">Add session</button>
</form>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="<%=request.getContextPath()%>/resources/js/vendor/jquery-ui.min.js"></script>
<script src="<%=request.getContextPath()%>/resources/js/plugins.js"></script>
<script src="<%=request.getContextPath()%>/resources/js/main.js"></script>

</body>

这是我的 Controller :

//Gets the information from the new session form.
@RequestMapping(value = "/addsessionresult")
@ResponseBody
public ModelAndView addSession(@ModelAttribute("session") Session session, BindingResult result){
try {
//session's start_time, end_time, start_date, end_date are null here. All other variables are loaded correctly.
DatabaseAdapter.executeSQLUpdate("INSERT INTO Session (Course, Name, StartTime, EndTime, Frequency, Staff, MaxAttendance, Compulsory, Venue, StartDate, EndDate) "
+ "VALUES (" +
session.getCourse() + ", " +
session.getSession_name() + ", " +
session.getStart_time() + ", " +
session.getSession_end_time() + ", " +
session.getSession_frequency() + ", " +
session.getStaff_member() + ", " +
session.getMax_attendance() + ", " +
session.isCompulsory() + ", " +
session.getVenue() + ", " +
session.getStart_date() + ", " +
session.getEnd_date() + ", " +
")");
//return "Added successfully!";
return new ModelAndView("addsessionresult", "result", "User was added");
} catch (SQLException e) {
//return "Adding failed! Error: " + e.getMessage();
return new ModelAndView("addsessionresult", "result", "User not added\r\n\r\nError: \n" +e.getMessage());
}
}

这是我的 session 类:

import java.util.Date;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class Session {
private String course, session_name;
private Calendar start_date, end_date;
private int session_frequency;
private Time start_time, end_time;
private String staff_member;
private int max_attendance, session_duration;
private boolean compulsory;
private String venue;

public ArrayList<Calendar> getDates(){
ArrayList<Calendar> dates = new ArrayList<Calendar>();
if (session_frequency > 0) {
for (Calendar d = start_date; !d.after(end_date); d.add(Calendar.DATE, session_frequency)){
dates.add((Calendar) d.clone());
}
} else {
dates.add(start_date);
}
return dates;
}

public void setSession_duration(Time start_time, Time end_time) {
//TODO probably needs fixing.
int i = 0;
}

public String[] toArray(){
String[] result = new String[6];
result[0] = "Course: " + course;
result[1] = "Session: " + session_name;
result[2] = "Time: " + start_time.toString().substring(0, 5) + " - " + end_time.toString().substring(0, 5);
result[3] = "Staff: " + staff_member;
if (compulsory) {
result[4] = "Attendance is compulsory";
} else {
result[4] = "Attendance is not compulsory";
}
result[5] = "Venue: " + venue;

return result;
}

public String[] toArray(Calendar date){
SimpleDateFormat sdfDate = new SimpleDateFormat("dd MMM yyyy");
String[] result = new String[7];
result[0] = "Course: " + course;
result[1] = "Session: " + session_name;
result[2] = "Date: " + sdfDate.format(date.getTime());
result[3] = "Time: " + start_time.toString().substring(0, 5) + " - " + end_time.toString().substring(0, 5);
result[4] = "Staff: " + staff_member;
if (compulsory) {
result[5] = "Attendance is compulsory";
} else {
result[5] = "Attendance is not compulsory";
}
result[6] = "Venue: " + venue;

return result;
}

public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getSession_name() {
return session_name;
}
public void setSession_name(String session_name) {
this.session_name = session_name;
}
public Calendar getStart_date() {
return start_date;
}
public void setStart_date(Calendar start_date) {
this.start_date = start_date;
}
public void setStart_date(String start_date) {
System.out.println("Damn, start date");
try {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat();
cal.setTime(sdf.parse(start_date));
setStart_date(cal);
} catch (Exception e) { }
}
public Calendar getEnd_date() {
return end_date;
}
public void setEnd_date(Calendar end_date) {
this.end_date = end_date;
}
public void setEnd_date(String end_date) {
try {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat();
cal.setTime(sdf.parse(end_date));
setEnd_date(cal);
} catch (Exception e) { }
}
public int getSession_frequency() {
return session_frequency;
}
public void setSession_frequency(int session_frequency) {
//Transform to weeks.
this.session_frequency = 7 * session_frequency;
}
public Time getStart_time() {
return start_time;
}
public void setStart_time(String start_time) {
this.start_time = Time.valueOf(start_time);
}
public void setStart_time(Time start_time) {
this.start_time = start_time;
}
public Time getSession_end_time() {
return end_time;
}
public void setSession_end_time(Time session_end_time) {
this.end_time = session_end_time;
}
public int getSession_duration() {
return session_duration;
}
public void setSession_duration(int session_duration) {
//I hate Java's Time / Date / Calendar / etc. classes!!!
//Update end time.
/*Calendar cal = Calendar.getInstance();
cal.setTime(start_time);
cal.add(Calendar.MINUTE, session_duration);
end_time.setTime(cal.getTimeInMillis());
*/
this.session_duration = session_duration;
}
public String getStaff_member() {
return staff_member;
}
public void setStaff_member(String staff_member) {
this.staff_member = staff_member;
}
public int getMax_attendance() {
return max_attendance;
}
public void setMax_attendance(int max_attendance) {
this.max_attendance = max_attendance;
}
public boolean isCompulsory() {
return compulsory;
}
public void setCompulsory(boolean compulsory) {
this.compulsory = compulsory;
}
public String getVenue() {
return venue;
}
public void setVenue(String venue) {
this.venue = venue;
}
}

最佳答案

不要有两个以相同方式命名的 setter,并采用不同的参数类型。重命名其中之一后,请确保您的 JSP 使用与采用字符串作为参数的 setter 相对应的名称。

关于java - 在 Spring 中向对象添加日历值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20441702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com