java - Spring MVC 初学者
问题描述
我使用 JSP 和 Spring MVC 制作了注册页面。但是我想在运行应用程序时显示此页面而不提供任何方法、操作、命令名称...我的页面如下:
<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>
但它不显示它会给出异常,例如:
java.lang.IllegalStateException:Bean 名称“命令”的 BindingResult 和普通目标对象都不能用作请求属性
请在这件事上给予我帮助....
提前致谢
解决方案
您必须添加一个 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了解更多详情
推荐阅读
- c# - 元素出现在数组中的频率。代码有效,但正在寻找更好的答案
- bash - bash 在 echo -e 格式化文本上使用列
- xml - 使用 XSLT 将 XML 输出转换为新结构化的(其他元素)XML 时,如何在 XML 根标记中包含元素
- plc - 西门子PLC编程最佳实践
- intellij-idea - 如何在 IntelliJ for MAC 中重新格式化特征文件中的列
- visual-studio - SSAS 表格部署
- ios - 动画约束或变换有什么区别?
- sql-server - 导入到新服务器上的同一个表时,SQL Server 导出到 CSV 失败
- javascript - Parsing JSON for multiple categories for TVOS
- r - 在 flexdashboard 中使用 highchart 渲染交互式树形图