java - 如何在启动时设置一个简单的 Spring 表单

标签 java spring jsp spring-mvc servlets

我是 Spring 新手,正在学习基本的 hello world 应用程序。这是我遇到的疑问。如何将初始加载页面设置为index.jsp。我的index.jsp位于WebContent文件夹中。但是每次我从eclipse运行index.jsp来查看该页面。

http://localhost:8080/TestingSpring/index.jsp

但是当我运行该项目时,它会

http://localhost:8080/TestingSpring/

并给出404。每次我都必须在项目中显式运行index.jsp才能获取 View 。

另外,我的第二个疑问是在我的index.jsp中,我使用了 打个招呼

我的 Controller 类包含

@RequestMapping("/contacts")
public ModelAndView showContacts() {

    return new ModelAndView("contacts", "command", new Contact());
}

从重定向中,它使用 spring 表单加载 contact.jsp,这是我的 contact.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>


 <title>Contact Manager</title>

<h2>Contact Manager</h2>
<form:form method="post" action="addContact.html">
<br>
<table>
<tbody><tr>
    <td><form:label path="firstname">First Name</form:label></td>
    <td><form:input path="firstname"></form:input></td> 
</tr>
<tr>
    <td><form:label path="lastname">Last Name</form:label></td>
    <td><form:input path="lastname"></form:input></td>
</tr>
<tr>
    <td><form:label path="lastname">Email</form:label></td>
    <td><form:input path="email"></form:input></td>
</tr>
<tr>
    <td><form:label path="lastname">Telephone</form:label></td>
    <td><form:input path="telephone"></form:input></td>
</tr>
<tr>
    <td colspan="2">
        <input type="submit" value="Add Contact">
    </td>
</tr>

我如何在没有任何重定向的情况下直接加载此页面。现在从index.jsp --> contact.jsp 它将显示 Spring 表单。如何直接执行此操作。 我怎样才能直接在我的index.jsp中包含spring表单。

共享 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 <display-name>Spring3MVC</display-name> 
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

最佳答案

根据您的 Dispatcher Servlet Url 映射更改您的 RequestMapping。

在您的情况下,您的 Dispatcher Servlet URL 模式是 *.html.. 因此,请像这样更改您的 RequestMapping...

@RequestMapping(value = "/contacts.html")
public ModelAndView showContacts() { }

关于java - 如何在启动时设置一个简单的 Spring 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30473449/

相关文章:

java - 声明一个二叉树,它采用包含通用类型键值的通用类型节点?

java - Micrometer - Prometheus Gauge 显示 NaN

java - 打破java泛型命名约定?

java - Hibernate 只加载列表中的一个对象

jsp - web.xml 中的以下条目指的是什么?

security - 阻止直接访问 Struts2 中的 *.jsp 和 *.action

java - 从java获取gradle构建路径文件夹

java - Spring 启动 |多个调度程序 Servlet

spring - 在 Spring 中对 @NumberFormat 使用不同的区域设置

html - 字体系列样式在 IE8 和 IE9 中不起作用