Java 如何在启动时设置一个简单的spring表单
我是spring的新手,正在学习一个基本的hello world应用程序。如何将初始加载页面设置为index.jsp。我的index.jsp位于WebContent文件夹中。但是每次我都从eclipse运行index.jsp来查看页面Java 如何在启动时设置一个简单的spring表单,java,spring,jsp,spring-mvc,servlets,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/ 每次我都必须在项目中显式运行index.jsp来获取视图 我的第
http://localhost:8080/TestingSpring/index.jsp
然而,当我运行该项目时,它将
http://localhost:8080/TestingSpring/
每次我都必须在项目中显式运行index.jsp来获取视图
我的第二个疑问是在index.jsp中,我使用了
打招呼
我的控制器类包含
@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将显示te 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>
Spring3MVC
index.jsp
春天
org.springframework.web.servlet.DispatcherServlet
1.
春天
*.html
1)尝试将url更改为
在web.xml中,您可以将index.jsp更改为您想要的jsp,如下所示,因为您需要contacts.jsp,我只使用了它
<welcome-file-list>
<welcome-file>contacts.jsp</welcome-file>
</welcome-file-list>
contacts.jsp
更改为您想要的jsp页面
同时将Servlet映射更改为
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
春天
/*
关闭contacts.jsp中的标记,并在web.xml的中指定contacts.jsp根据Dispatcher Servlet Url映射更改请求映射
在您的情况下,Dispatcher Servlet URL模式是*.html。。所以像这样更改您的请求映射
@RequestMapping(value = "/contacts.html")
public ModelAndView showContacts() { }
但是我没有给我index.jsp,它仍然给我404。只有当我显式运行index.jsp时,它才被加载。因此,除非它被重定向,否则我无法在index.jsp中添加spring表单。我在这里做错了什么?这是因为在你的web.xml中,你给了你的servelet映射错误。请将其更改为,如answerHTTP状态404所示-/我正在测试spring/contacts/contacts.jsp,但没有成功。我没有定义任何applicationContext,这有问题吗?@DilipKumar共享了web.xml