我仍然是 Spring 和 Web 编程的初学者。
我正在尝试创建一个将 modelAttribute 发送到另一个页面但 modelAttribute 始终为空的小表单。
文件上传表单.java
public class FileUploadForm {
CommonsMultipartFile file; String blabla;
// initiation of FileUploadForm.
public FileUploadForm() {
// TODO Auto-generated constructor stub
file= null;
}
public String getBlabla() {
return blabla;
}
public void setBlabla( String blabla ) {
this.blabla = blabla;
}
public CommonsMultipartFile getFile() {
return file;
}
public void setFile(CommonsMultipartFile file) {
this.file = file;
}
}
我的上传测试.jsp
<script>
function addFileToEcc() {
document.getElementById('file_upload_form').target = 'upload_target';
};
</script>
<div>
<div id="titleSection" style="padding:5px;color:#303030;"class="ui-state-active, ui-widget-content ui-state-active, ui-widget-header ui-state-active" onclick="$('#fourthSection').slideToggle();" >
<label>detail</label>
</div>
<div id='AttachSection'>
<form:form id="file_upload_form" action="NewfileUpload.do" method="post" modelAttribute="fileUploadform" enctype="multipart/form-data">
<input type="text" id='blabla' name="blabla" value="boubou" />
<iframe id="upload_target" name="upload_target" src=""></iframe>
</form:form>
</div>
</div>
文件上传控制器.java
@Controller
@SessionAttributes
public class FileUpload extends ServiceImpl {
@Autowired
Log log;
@Autowired
FileManagementService gFM;
@RequestMapping( value = "/NewfileUpload.do", method = RequestMethod.POST )
// public ModelAndView uploadFile( @ModelAttribute FileUploadForm mainForm, HttpServletRequest pRequest ) {
public ModelAndView uploadFile( @ModelAttribute("fileUploadform") FileUploadForm mainForm, HttpServletRequest pRequest ) {
log.info( "Begin File Upload." );
FileUploadDetails fud = new FileUploadDetails();
fud.setStatusCode( FileUploadDetails.STATUS_SUCCESS );
return new ModelAndView( "fileUploaded", "fud", fud );
}
}
我知道代码看起来不漂亮并且会抛出错误,但是我在 FileUploadController.java 文件中设置了一个断点来检查传递给它的参数中的内容,并且 mainForm 总是返回 null。
任何人都知道为什么它会为空,而我知道我的 inputtext id="blabla" 确实有文本。
谢谢你的帮助