我正在将选择列表值传递给我的动作,但是在对其进行调试之后,我发现我的动作上的变量具有空值,看来它没有将选择列表的选定值传递给动作 视图如下:-
enter <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sb" uri="/struts-bootstrap-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="assets/css/bootstrap-united.css" rel="stylesheet" />
<link href="bootstrap/css/bootstrap-responsive.css" rel="stylesheet" />
<style>
body {
height: 100%;
margin: 0;
background: url(assets/img/books.jpg);
background-size: 1440px 800px;
background-repeat: no-repeat;
display: compact;
color: black;
}
#div1 {
color: "black";
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form class="navbar-form navbar-right" action="course" method="get">
<s:select name="selectedCourse" label="Courses available"
headerKey="-1" list="availableCourse"
headerValue="Choose a course"></s:select>
</div>
<s:a class="btn btn-primary" href="course">View Course » </s:a>
<a class="btn btn-primary" href="course-input">View Course » </a>
</div>
<div></div>
</div>
</form>
<script src="jquery-1.8.3.js">
</script>
<script src="bootstrap/js/bootstrap.js">
</script>
</body>
</html>
我的操作如下:-我遵循了所有命名约定,似乎每次都没有调用setter,但是在调试时看到的任何方法都称为getter。
package com.trainingbasket.ssms.actions;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import com.trainingbasket.ssms.service.CourseService;
/*It initializes the course list from Database and after course is selected
* displays the description as recorded in DB to the user. */
@SuppressWarnings("serial")
public class CourseAction extends ActionSupport {
private CourseAction courseAction;
private String selectedCourse;// selectedCourse
public List<String> getAvailableCourse() {
return availableCourse;
}
public void setAvailableCourse(List<String> availableCourse) {
this.availableCourse = availableCourse;
}
private List<String> availableCourse;
@Action("course-input")
public String input() throws Exception {
return "course";
}
@Action(value = "course", results = {
@Result(name = "success", location = "courseDetail", type = "redirect"),
@Result(name = "failure", location = "course", type = "redirect") })
// @Action("course")
@Override
public String execute() throws Exception {
String result = "";
CourseService courseService = new CourseService();
if (selectedCourse != null) {
result = courseService.findByName(selectedCourse);
if (result.equals("Course FoundSuccess")) {
System.out.println("in success");
return "success";
} else {
System.out.println("in failure");
return "failure";
}
}
return SUCCESS; // need to change as per logic
}
public CourseAction() {
availableCourse = new ArrayList<String>(); // need to fetch courseNAmes from the DB into this list;
/* hard coding up till DB problem is solved */
availableCourse.add("Java");
availableCourse.add("Python");
availableCourse.add("Big Data");
availableCourse.add("Machine Learning");
availableCourse.add("Guru");
}
public String getSelectedCourse() {
System.out.println(selectedCourse);
return selectedCourse;
}
@RequiredStringValidator(type = ValidatorType.FIELD, message = "Course is a required field")
public void setSelectedCourse(String selectedCourse) {
System.out.println("dsdsds"+selectedCourse);
this.selectedCourse = selectedCourse;
}
}