我有以下课程:
public class MyDTO {
private String kiosk;
...
}
Run Code Online (Sandbox Code Playgroud)
和以下网址:
http://localhost:1234/mvc/controllerUrl?kiosk=false
Run Code Online (Sandbox Code Playgroud)
并遵循控制器方法:
@RequestMapping(method = RequestMethod.GET, produces = APPLICATION_JSON)
@ResponseBody
public ResponseEntity<List<?>> getRequestSupportKludge(final MyDTO myDTO, BindingResult bindingResult) {
...
}
Run Code Online (Sandbox Code Playgroud)
现在它工作得很好,布尔字段正确解析.
现在url参数已经改变如下:
http://localhost:1234/mvc/controllerUrl?new_kiosk=false
Run Code Online (Sandbox Code Playgroud)
我不想在DTO中更改参数名称.有没有办法说spring知道new_kiosk请求参数值应该放在kiosk字段中?
我有以下控制器返回视图:
@RequestMapping(value = "/admin/adminUsers", method = RequestMethod.GET)
public String adminUsers(ModelMap model, HttpSession session) {
Set<TerminalAdmin> users = terminalAdminService.getAllAdmins();
session.setAttribute("users", users);
model.addAttribute("adminRoles", terminalAdminService.findAllAdminRoles());
model.addAttribute("terminalAdmin", new TerminalAdmin());
model.addAttribute("generatedPassword", PasswordUpdateStatus.generatePassword());
return "admin/adminUsers";
}
terminalAdminService.findAllAdminRoles()
Run Code Online (Sandbox Code Playgroud)
返回带有id的集合:
在jsp上我渲染它像这样:
<form:form modelAttribute="terminalAdmin" action="/admin/addNewAdmin">
...
<form:checkboxes items="${adminRoles}" path="adminRoles"/>
...
</form:form>
Run Code Online (Sandbox Code Playgroud)
我有以下控制器来接受这个对象:
@RequestMapping(value = "/admin/addNewAdmin")
public String adminUsers(@ModelAttribute @Valid TerminalAdmin terminalAdmin...){
....
}
Run Code Online (Sandbox Code Playgroud)
在调试中,我看到terminalAdminadminRoles没有id.
如何解决这个问题?
PS
我正在对一个非常旧的应用程序进行更改。这是使用 Spring MVC 4。
我需要在 Spring 控制器的 JSP 中从 form:form 标签中发布数据。UI 是固定的,我只能在服务器端进行更改。基于我提交的表单中的特定字段,我希望在我的 Controller Handler 方法参数中有正确的子对象实例。
例如,
class Payment {...}
class CardPayment extends Payment{...}
class CheckPayment extends Payment{...}
Run Code Online (Sandbox Code Playgroud)
在 UI 表单中,会有一个名为 paymentType 的输入值。付款将是 commandObject 或 ModelAttribute
我希望我的 @PostMapping 控制器在参数中具有正确的子对象。我不想在控制器代码中手动实例化它。
@PostMapping
public ModelAndView doSomePay(@ModelAttribute("payment") Payment paymentInput, BindingResult result){
Run Code Online (Sandbox Code Playgroud)
现在我希望paymentInput上面的这个对象是 CardPayment 或 checkPayment 类型。
我试图创建一个@initBinder and WebDatabinder但实际上我有近 10 个子类,我是否需要为所有这些创建“编辑器”?
如果是,那么短而快速地创建 propertyEditor 的最佳方法是什么
@InitBinder
public void initBinder(WebDataBinder binder, HttpServletRequest request) {
String paymentType = request.getParameter("paymentType");
PropertyEditor productEditor;
//somehow I can …Run Code Online (Sandbox Code Playgroud) 我刚刚实现了AspectJ,如下所述:https://stackoverflow.com/a/10998044/2182503
这个解决方案工作正常,直到我发现我的@Autowired字段为空@InitBinder.这些字段在该字段中仅为空@InitBinder.
@Controller
public class EmployeeController {
@Autowired private GenericDaoImpl<Role, Integer> roleDao;
@Autowired private GenericDaoImpl<Employee, Integer> employeeDao;
@Autowired private EmployeeValidator employeeValidator;
@InitBinder
private void initBinder(WebDataBinder binder) {
// autowired fields are null
binder.setValidator(employeeValidator);
binder.registerCustomEditor(Set.class, "roles", new CustomCollectionEditor(Set.class) {
protected Object convertElement(Object element) {
if (element != null) {
Integer id = new Integer((String) element);
Role role = roleDao.findById(id);
return role;
}
return null;
}
});
}
@PreAuthorize("hasRole('MASTERDATA_VIEW')")
@RequestMapping(value = { "/employees" …Run Code Online (Sandbox Code Playgroud) spring aspectj spring-mvc spring-mvc-initbinders aspectj-maven-plugin
我有以下控制器方法:
@RequestMapping(value = "/owner/terminals/save", method = RequestMethod.POST)
public String saveTerminal( @RequestParam(value = "name") String name,
@RequestParam(value = "file") @Valid OnlyForImagesFileWrapper file,
BindingResult bindingResult )
{
...
Run Code Online (Sandbox Code Playgroud)
我看到以下堆栈跟踪:
org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile' to required type 'com.terminal.domain.validation.OnlyForImagesFileWrapper'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [com.terminal.domain.validation.OnlyForImagesFileWrapper]: no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:74)
....
Caused by: java.lang.IllegalStateException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [com.terminal.domain.validation.OnlyForImagesFileWrapper]: no matching editors or …Run Code Online (Sandbox Code Playgroud) 嗨,我是spring框架的新手。我做了一个小例子,尝试使用spring验证api验证输入字段。这是代码
@RequestMapping(value = "/applicationFormSubmit", method = RequestMethod.POST)
public String insertdata( @ModelAttribute("applicationForm") @Valid ApplicationFormBean applicationFormBean, @RequestParam("file") MultipartFile file, BindingResult result,Model model)
{
if(result.hasErrors())
{
return "applicationForm";
}
try {
Blob blob = Hibernate.createBlob(file.getInputStream());
// applicationFormBean..setFilename(file.getOriginalFilename());
applicationFormBean.setSignature(blob);
// applicationFormBean.setContentType(file.getContentType());
} catch (IOException e) {
e.printStackTrace();
}
applicationFormUserService.insertApplicationData(applicationFormBean);
return "applicationForm";
}
Run Code Online (Sandbox Code Playgroud)
但是当我用空白值提交from时,它给了我以下错误
Field error in object 'applicationForm' on field 'applicantName': rejected value []; codes [NotEmpty.applicationForm.applicantName,NotEmpty.applicantName,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [applicationForm.applicantName,applicantName]; arguments []; default message [applicantName]]; default message [Please enter your nnnn.]
Field error in …Run Code Online (Sandbox Code Playgroud) 我有一个控制器工作正常,它可以注册和更新一个实体,以及如何分别创建表格以分别保存和更新实体
@RequestMapping(value="/registrar.htm", method=RequestMethod.GET)
public String crearRegistrarFormulario(Model model){
…
}
@RequestMapping(value="/{id}/actualizar.htm", method=RequestMethod.GET)
public String crearActualizarFormulario(@PathVariable("id") String id, Model model){
…
}
Run Code Online (Sandbox Code Playgroud)
直到这里我没有问题.
我的问题是关于@InitBinder
我需要使用相同的实体Deportista(运动员),一个特殊的设置来保存和更新.例如
@InitBinder
public void registrarInitBinder(WebDataBinder binder) { // register or save
logger.info(">>>>>>>> registrarInitBinder >>>>>>>>>>>>>");
…
CustomDateEditor customDateEditor = new CustomDateEditor(...
…
}
@InitBinder
public void actualizarInitBinder(WebDataBinder binder) { // update
logger.info(">>>>>>>> actualizarInitBinder >>>>>>>>>>>>>");
…
CustomDateEditor customDateEditor = new CustomDateEditor(...
…
binder.setDisallowedFields(…) //I need this only for update
}
Run Code Online (Sandbox Code Playgroud)
我看过以下内容:
这些链接中提到变通不同entities,例如 …
spring-mvc ×7
spring ×6
java ×2
spring-form ×2
aspectj ×1
hibernate ×1
spring-3 ×1
spring-4 ×1