首页 > 解决方案 > 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 和普通目标对象都不能用作请求属性

请在这件事上给予我帮助....

提前致谢

标签: javaspring-mvc

解决方案


您必须添加一个 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了解更多详情


推荐阅读