Java Spring MVC:在使用';时获取完整的对象而不是字符串;选择选项';
我有一个类Java Spring MVC:在使用';时获取完整的对象而不是字符串;选择选项';,java,spring,spring-mvc,Java,Spring,Spring Mvc,我有一个类课程,它包含对课程对象的引用,如下所示: public class Lesson { ... private Course course; ... public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } ... } 我想通过选择表单
课程
,它包含对课程
对象的引用,如下所示:
public class Lesson {
...
private Course course;
...
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
...
}
我想通过选择表单在课程
对象上设置课程
属性:
<form:form method="post" action="addLesson" modelAttribute="lesson">
<form:select path="course">
<form:options items="${courses}"/>
</form:select>
<input type="submit" name="addLesson" value="Add lesson">
</form:form>
问题是它将课程
对象(由toString()
定义)的字符串
表示形式传递给课程
对象的课程
设置器
如何使用我的选择表单正确设置课程
对象的课程
属性?根据您需要设置项目值
和项目标签
在表单:选项
标记上,否则,如您所述,该值将是对象的toString()
,itemValue
和itemLable
应该引用课程
bean中的属性
假设你的课程
类有一个属性名称
,那么你的表单应该如下所示:
<form:form method="post" action="addLesson" modelAttribute="lesson">
<form:select path="course">
<form:options items="${courses}" itemValue="name" itemLabel="name"/>
</form:select>
<input type="submit" name="addLesson" value="Add lesson">
</form:form>
根据需要,您需要在表单:options
标签上设置itemValue
和itemlab
,否则,正如您已经提到的,该值将是对象的toString()
,itemValue
和itemlab
应该引用课程
bean中的属性
假设你的课程
类有一个属性名称
,那么你的表单应该如下所示:
<form:form method="post" action="addLesson" modelAttribute="lesson">
<form:select path="course">
<form:options items="${courses}" itemValue="name" itemLabel="name"/>
</form:select>
<input type="submit" name="addLesson" value="Add lesson">
</form:form>
根据需要,您需要在表单:options
标签上设置itemValue
和itemlab
,否则,正如您已经提到的,该值将是对象的toString()
,itemValue
和itemlab
应该引用课程
bean中的属性
假设你的课程
类有一个属性名称
,那么你的表单应该如下所示:
<form:form method="post" action="addLesson" modelAttribute="lesson">
<form:select path="course">
<form:options items="${courses}" itemValue="name" itemLabel="name"/>
</form:select>
<input type="submit" name="addLesson" value="Add lesson">
</form:form>
根据需要,您需要在表单:options
标签上设置itemValue
和itemlab
,否则,正如您已经提到的,该值将是对象的toString()
,itemValue
和itemlab
应该引用课程
bean中的属性
假设你的课程
类有一个属性名称
,那么你的表单应该如下所示:
<form:form method="post" action="addLesson" modelAttribute="lesson">
<form:select path="course">
<form:options items="${courses}" itemValue="name" itemLabel="name"/>
</form:select>
<input type="submit" name="addLesson" value="Add lesson">
</form:form>
您可以使用直接绑定课程对象,而不是某些课程对象属性 实现转换器接口,在您的情况下,该接口可以将选定的courseName转换为课程:
public class CourseConverter implements Converter<String, Course> {
public Course convert(String source) {
List<Course> courseList = //Populate courseList the way you did in Lesson
Course course = //Get course object based on selected courseName from courseList;
return course;
}
}
公共类CourseConverter实现转换器{
公共课程转换(字符串源){
List courseList=//按照第一课中的方式填充courseList
Course Course=//根据courseList中选择的courseName获取课程对象;
返回路线;
}
}
现在注册转换器:
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" >
<property name="converters">
<set>
<bean class="your.package.CourseConverter"/>
</set>
</property>
</bean>
并将您的表单:选项更改为:
<form:options items="${courses}" itemValue="courseName" itemLabel="courseName"/>
您可以使用直接绑定课程对象,而不是某些课程对象属性 实现转换器接口,在您的情况下,该接口可以将选定的courseName转换为课程:
public class CourseConverter implements Converter<String, Course> {
public Course convert(String source) {
List<Course> courseList = //Populate courseList the way you did in Lesson
Course course = //Get course object based on selected courseName from courseList;
return course;
}
}
公共类CourseConverter实现转换器{
公共课程转换(字符串源){
List courseList=//按照第一课中的方式填充courseList
Course Course=//根据courseList中选择的courseName获取课程对象;
返回路线;
}
}
现在注册转换器:
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" >
<property name="converters">
<set>
<bean class="your.package.CourseConverter"/>
</set>
</property>
</bean>
并将您的表单:选项更改为:
<form:options items="${courses}" itemValue="courseName" itemLabel="courseName"/>
您可以使用直接绑定课程对象,而不是某些课程对象属性 实现转换器接口,在您的情况下,该接口可以将选定的courseName转换为课程:
public class CourseConverter implements Converter<String, Course> {
public Course convert(String source) {
List<Course> courseList = //Populate courseList the way you did in Lesson
Course course = //Get course object based on selected courseName from courseList;
return course;
}
}
公共类CourseConverter实现转换器{
公共课程转换(字符串源){
List courseList=//按照第一课中的方式填充courseList
Course Course=//根据courseList中选择的courseName获取课程对象;
返回路线;
}
}
现在注册转换器:
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" >
<property name="converters">
<set>
<bean class="your.package.CourseConverter"/>
</set>
</property>
</bean>
并将您的表单:选项更改为:
<form:options items="${courses}" itemValue="courseName" itemLabel="courseName"/>
您可以使用直接绑定课程对象,而不是某些课程对象属性 实现转换器接口,在您的情况下,该接口可以将选定的courseName转换为课程:
public class CourseConverter implements Converter<String, Course> {
public Course convert(String source) {
List<Course> courseList = //Populate courseList the way you did in Lesson
Course course = //Get course object based on selected courseName from courseList;
return course;
}
}
公共类CourseConverter实现转换器{
公共课程转换(字符串源){
List courseList=//按照第一课中的方式填充courseList
Course Course=//根据courseList中选择的courseName获取课程对象;
返回路线;
}
}
现在注册转换器:
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" >
<property name="converters">
<set>
<bean class="your.package.CourseConverter"/>
</set>
</property>
</bean>
并将您的表单:选项更改为:
<form:options items="${courses}" itemValue="courseName" itemLabel="courseName"/>
通常用于UI呈现
格式化程序
与转换服务一起使用。但在Spring3之前,使用了属性编辑器
我已经为您的案例分享了github项目示例
@组件(“课程格式”)
公共类CourseFormatter实现格式化程序{
private CourseRepository CourseRepository=新课程Repository();
@凌驾
公共字符串打印(课程,区域设置arg1){
return-course.getName();
}
@凌驾
公共课程解析(字符串actorId,区域设置arg1){
return courseRepository.getCourse(actorId);
}
}
通常用于UI呈现格式化程序
与转换服务一起使用。但在Spring3之前,使用了属性编辑器
我已经为您的案例分享了github项目示例
@组件(“课程格式”)
公共图书馆