我有一个表单,其目的是允许用户在数据库表中创建一个新条目。表格非常大,总共约 50 个字段。我需要一种方法将所有这些值传递给我的 Controller ,尽管我看不到一种简单的方法。我见过的每个解决方案都是 @RequestParam('field')
,但有大约 50 个字段,这有点疯狂。也许如果使用 @RequestParam Map<T, T>
可能吗?
我最初尝试的是创建一个 AJAX POST 调用
baseapplication.com/add?field1=value1&field2=value2&...&field50=value50
但是随后 servlet 提示找不到 add.jsp
文件。这有点合理,因为该文件不存在,但我创建了一个 Controller 映射 @RequestMapping(value="/add")
所以我实际上不需要那个文件。我有另一种方法可以创建对 /search
的 AJAX GET 调用使用一些 url 参数,效果很好。也没有 search.jsp
文件。
这个问题很难解释,希望我做的是半途而废。现在让我们看一些代码,由于处理大约 50 个表单字段非常冗长,所以省略了一些代码。
从启动整个过程的 JavaScript 开始:
ctx = "baseapplication.com"
$('#addNewRecordBtn').on('click', function(){
var m_insId = document.getElementById('m_insId');
//This repeats for every field
var url = ctx + '/add?insuredId=' + m_insId /** + all other fields **/;
addCase(url);
});
function addCase(url) {
$.ajax({
url: url,
type: 'POST'
}).success(function(data){
alert("Successfully added row");
}).fail(function(jzXHR, textStatus, errorThrown){
alert(jzXHR);
alert(textStatus);
alert(errorThrown);
});
}
因此流程如下:用户点击 addNewRecordBtn
按钮,它触发第一个功能。此函数获取表单中每个字段的值,然后为每个值构建一个带有参数的 URL。 addCase()
然后调用函数,它创建一个 AJAX POST(不知道该怎么调用它?)到刚刚构建的 URL。此功能不成功,错误警报提供零信息,但服务器控制台声称 Failed to find resource /WEB-INF/views/add.jsp
现在我们进入 Controller 。
@Controller
public class ApplicationController {
@Autowired
SpecialClaimsCaseManager caseManager;
@RequestMapping(value="/add")
public void addRow(@RequestParam Map<String, String> requestParams) {
SpecialClaimsCase newCase = new SpecialClaimsCase();
newCase.setInsuredId(requestParams.get("insuredId"));
//Repeat this for all parameters
caseManager.addNewSpecialClaimsCase(newCase);
}
caseManager.addNewSpecialClaimsCase(newCase)
调用只是从这个模型对象中创建一个 DTO 对象,然后通过一些 Hibernate 魔法将该新对象添加到数据库中。除了它有效之外,我对那方面了解不多。
所以,我不确定我的做法是否正确。我听说有一种方法可以使用 Spring 的标记库将模型对象映射到 JSP 表单,但是由于表单很大,因此需要大量重写。我也在使用 Bootstrap 来构建界面,我不确定 Bootstrap 和 Spring 的标签库是否能很好地结合。我无法想象为什么不。
我不确定是否需要在此处使用 AJAX。我选择了它,因为我不希望页面必须重新加载或进行任何操作。我通常不是 Web 开发人员,所以我确信我在这里缺乏一些基础知识。
我的主要问题是:鉴于我的情况,将这种海量信息导入我的 Controller 的最佳方式是什么?
在此先感谢您阅读这面文字和代码墙,并感谢您提供的任何帮助!
最佳答案
创建一个包含所有这些必需字段的域类,并生成 getter 和 setter 以及一个构造函数。一旦你获得所有这些字段/其中一些字段作为 json 发布到你的 Controller 。然后适当的 Controller 将调用所需的服务,然后 DAO 将处理持久性部分。 简而言之,将您需要的数据作为 JSON 对象发送。 json 将被制成 java 对象,并对其执行操作。
这是 Controller
@Controller
@RequestMapping(value = "/students/association")
public class StudentDepartmentController {
@Autowired
private StudentService studentService;
@Autowired
private StudentDepartmentService studentDepartmentService;
@RequestMapping(value = "/add-department", method = RequestMethod.POST)
public ResponseEntity<StudentDepartment> createStudentDepartmentAssociation(
@RequestBody final StudentDepartment studentDepartment) {
StudentDepartment newStudentDepartment;
// check if the student exists
Student student = studentService.getStudentByUuid(studentDepartment
.getStudentUuid().getUuid());
if (null == student) {
throw new IllegalArgumentException("No students found!");
}
// check the status of student
if (student.getStatus() == Liveliness.INACTIVE) {
throw new IllegalArgumentException(
"cannot create an association with an inactive student! activate student first");
}
// check for valid department
if (null == studentDepartment.getDepartment().getName()) {
throw new IllegalArgumentException("No such Department");
}
// check if the association already exists
if (null != findOneAssociationAgainstStudent(student)) {
throw new IllegalArgumentException(
"cannot create student department association, as "
+ student.getUsn()
+ " already present in another association ( "
+ studentDepartment.getDepartment().getName()
+ " )");
}
try {
newStudentDepartment = studentDepartmentService
.createNewAssociation(studentDepartment);
} catch (DataIntegrityViolationException ex) {
throw new AutomationTransactionException(
"cannot create student department association, as "
+ student.getUsn()
+ " already present in another association ( "
+ studentDepartment.getDepartment().getName()
+ " )", ex);
}
return new ResponseEntity<StudentDepartment>(newStudentDepartment,
HttpStatus.CREATED);
}
private StudentDepartment findOneAssociationAgainstStudent(Student student) {
return studentDepartmentService.findOneAssociation(student);
}
private StudentDepartment findOne(Uuid uuid) {
String studentDepartmentUuid = uuid.getUuid();
return findOne(studentDepartmentUuid);
}
private StudentDepartment findOne(String uuid) {
return studentDepartmentService.findOne(uuid);
}
@RequestMapping(value = "/delete-association", method = RequestMethod.DELETE)
public ResponseEntity<String> deleteStudentDepartmentAssociationByUuid(
@RequestBody final StudentDepartment studentDepartment) {
// check if association exists
StudentDepartment association = findOne(studentDepartment.getUuid());
if (null == association) {
throw new IllegalArgumentException("No such association found!");
}
studentDepartmentService.deleteAssociation(association);
return new ResponseEntity<String>("success", HttpStatus.OK);
}
}`
@RequestBody
注解帮助你把json对象变成java对象。
这样,您可以将有效载荷作为 json,获取 java 对象并使用 ResponseEntity<Class>
将 json 发送回 UI。注释
关于java - 获取大型表单到 Spring Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25042247/