我使用JSP和Spring MVC制作了注册页面。但我想在运行应用程序时显示此页面而不提供任何方法,操作,commandName ... 我的页面如下:
<body>
<h2>Message is: ${message}</h2>
<form:form>
<div class="table-responsive">
<table class="table table-bordered" style="width: 300px">
<tr>
<td>Id :</td>
<td><form:input type="text" path="id" /></td>
</tr>
<tr>
<td>Name :</td>
<td><form:input type="text" path="name" /></td>
</tr>
<tr>
<td>Age :</td>
<td><form:input type="text" path="age" /></td>
</tr>
<tr>
<td>Department :</td>
<td><form:input type="text" path="dept" /></td>
</tr>
<tr>
<td></td>
<td><input class="btn btn-primary btn-sm" type="submit" value="Submit" /></td>
</tr>
</table>
</div>
</form:form>
</body>
但它没有显示它会给出Exception,如:
java.lang.IllegalStateException:BindingResult和bean名称'command'的普通目标对象都不可用作请求属性
请帮助我......
提前致谢
答案 0 :(得分:0)
您必须添加一个PageController类,它将调用您的page.jsp文件。
@Controller
public class PageController {
@RequestMapping(value ="/")
public ModelAndView index() {
ModelAndView mv = new ModelAndView("page");
mv.addObject("message", "Hello world");
return mv;
}
}
您还需要一个DispatcherServlet的web.xml文件,它将是您的前端控制器
<web-app id = "WebApp_ID" version = "2.4"
xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
</web-app>
您还需要HelloWeb-servlet.xml文件进行映射
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package = "your package name which has page controller class" />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/" />
<property name = "suffix" value = ".jsp" />
</bean>
</beans>
附录 - 您可以参考https://www.tutorialspoint.com/spring/spring_web_mvc_framework.htm了解更多详情