【问题标题】:Spring How to redirect when button clicked and how to print to console when another button clicked?Spring单击按钮时如何重定向以及单击另一个按钮时如何打印到控制台?
【发布时间】: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>

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

  1. 在index.html中,为什么href方法有效而button方法重定向无效?

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

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

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

【问题讨论】:

    标签: java spring eclipse spring-mvc tomcat


    【解决方案1】:

    发现了我的问题。 Tomcat 部署良好,但是当我打开页面时,浏览器会加载缓存页面。

    【讨论】:

      【解决方案2】:

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

      使用 xml 配置禁用缓存:

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

      使用 java config 禁用缓存:

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

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

      【讨论】:

      • 谢谢,这个我只能用时间确认,因为有点随机。有时会正确加载,有时会加载缓存页面。
      • 您的 xml 配置只有一个问题。属性名称是“可缓存的”而不是“缓存”。除此之外,工作得很好。谢谢
      • 对不起,毕竟它会继续加载缓存页面。
      猜你喜欢
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 2021-09-07
      • 2020-01-26
      相关资源
      最近更新 更多