【发布时间】:2018-02-23 12:38:13
【问题描述】:
这是我的控制器:
@Controller
public class StoreController {
@RequestMapping("/index")
public String index() {
return "index";
}
@RequestMapping("/addProduct")
public String addProduct() {
return "addProduct";
}
@RequestMapping("/print")
public void print() {
System.out.println("Printed");
}
@RequestMapping("/redirectToAddProd")
public String redirectToAddProd() {
return "redirect:addProduct";
}
}
这是我的 index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Store</title>
</head>
<body>
<a href="addProduct.html">Add new product</a>
<form action="#" th:action="@{/redirectToAddProd}">
<input type="submit" value="Submit"/>
</form>
</body>
</html>
这是我的 addProduct.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Add a product</title>
</head>
<body>
<form action="#" th:action="@{/print}">
<button type="submit">Submit</button>
</form>
</body>
</html>
我的 store-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.ecommerce.store" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/static/" />
<property name="suffix" value=".html" />
</bean>
</beans>
最后是我的 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" version="3.0">
<display-name>MyStore</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>store</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>store</servlet-name>
<url-pattern>/index.html</url-pattern>
<url-pattern>/addProduct.html</url-pattern>
</servlet-mapping>
</web-app>
所以,我遇到了两个问题。
在index.html中,为什么href方法有效而button方法重定向无效?
在 addProduct.html 中按下按钮时如何打印到控制台?
谢谢你们,希望你们能帮助我。
编辑: 嘿,伙计们,我发现了一件事情。我的问题可能与Tomcat有关。 我不知道为什么,但有时我的 Tomcat 没有部署。 在我的 index.html 中,我添加了一个新按钮,例如,发布到服务器,运行服务器并且该按钮没有出现。有人知道对此有任何解决方案吗?我禁用了自动部署(但自动部署不起作用)。 我在 Spring Tool Suite (eclipse) 上运行 谢谢
【问题讨论】:
标签: java spring eclipse spring-mvc tomcat