gpt4 book ai didi

java - Spring 如何在单击按钮时重定向以及如何在单击另一个按钮时打印到控制台?

转载 作者:行者123 更新时间:2023-11-28 22:16:33 25 4
gpt4 key购买 nike

这是我的 Controller :

@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>

所以,我遇到了两个问题。

  1. 在index.html中,为什么href方法可以,button方法重定向不行?

  2. 在 addProduct.html 中,如何在按下按钮时打印到控制台?

谢谢你们,希望你们能帮助我。

编辑:嘿伙计们,我发现了一件事情。我的问题可能出在 Tomcat 上。我不知道为什么,但有时我的 Tomcat 没有部署。在我的 index.html 中,我添加了一个新按钮,例如,发布到服务器,运行服务器并且按钮没有出现。有人知道这个有什么解决办法吗?我禁用了自动部署(但自动部署不起作用)。我在 Spring Tool Suit (eclipse) 上运行谢谢

最佳答案

缓存问题可能是因为 thymeleaf 缓存配置。

使用 xml 配置禁用缓存:

<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<!-- Others configs here ... -->
<property name="cache" value="false" />
</bean>

使用 java 配置禁用缓存:

@Bean
public TemplateResolver templateResolver(){
TemplateResolver templateResolver = new ServletContextTemplateResolver();
// Others configs here ...
templateResolver.setCacheable(false);

return templateResolver;
}

Ps:只在开发环境禁用缓存

关于java - Spring 如何在单击按钮时重定向以及如何在单击另一个按钮时打印到控制台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46220605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com