是什么导致“java.lang.IllegalStateException:BindingResult或bean名称的普通目标对象‘命令’作为请求属性可用”?这意味着这是一个广泛的典型的问答文章,为这些类型的问题。我试图编写一个SpringMVC Web应用程序,用户可以在这个应用程序中将电影名称添加到内存中的集合中。它的配置如下public class Application extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] {};
}
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { SpringServletConfig.class };
}
protected String[] getServletMappings() {
return new String[] { "/" };
}}和@Configuration@ComponentScan("com.example")public class SpringServletConfig extends WebMvcConfigurationSupport {
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver vr = new InternalResourceViewResolver();
vr.setPrefix("WEB-INF/jsps/");
vr.setSuffix(".jsp");
return vr;
}}有一个@Controller类中的com.example包装@Controllerpublic class MovieController {
private final CopyOnWriteArrayList<Movie> movies = new CopyOnWriteArrayList<>();
@RequestMapping(path = "/movies", method = RequestMethod.GET)
public String homePage(Model model) {
model.addAttribute("movies", movies);
return "index";
}
@RequestMapping(path = "/movies", method = RequestMethod.POST)
public String upload(@ModelAttribute("movie") Movie movie, BindingResult errors) {
if (!errors.hasErrors()) {
movies.add(movie);
}
return "redirect:/movies";
}
public static class Movie {
private String filmName;
public String getFilmName() {
return filmName;
}
public void setFilmName(String filmName) {
this.filmName = filmName;
}
}}应用程序配置了上下文路径。/Example..当我发送GET请求时http://localhost:8080/Example/movies我期望JSP生成一个HTML<form>只有一个文本输入,用于Movie名称和提交按钮,我可以使用它发送带有新的POST请求。Movie..为什么JSP servlet不能呈现Spring的<form:form>标签?
3 回答
繁花不似锦
TA贡献1851条经验 获得超4个赞
ibeautiful
TA贡献1993条经验 获得超5个赞
modelAttribute="movie"
<form:input path="filmName" type="text" id="movie.name" />
- 3 回答
- 0 关注
- 3728 浏览
添加回答
举报
0/150
提交
取消