java - springMVC,如何删除链接(href)

标签 java spring jakarta-ee spring-mvc

package net.roseindia.controller;

import net.roseindia.service.ArticleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
@RequestMapping("/articles")
public class DeleteController {
    @Autowired
      private ArticleService articleService;

      @RequestMapping(value="/delete")
      public String deleteService(@RequestParam("ID") final Integer ids) {
          System.out.println("hello");
          articleService.deleteService(ids);

          return "redirect:/articles";

      }

}

可能在这里~~~~~~~~~~~~~~~~~~~~~~

<td><a href="/articles/delete.do?ID=${article.articleId}">delete</a></td>

这个问题让我很困惑。我认为可能是href的问题。Controller无法掌握href的链接

(第二次尝试)但似乎也不能像这样工作

import net.roseindia.service.ArticleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
@RequestMapping("/articles")
public class DeleteController {
    @Autowired
      private ArticleService articleService;

      @RequestMapping(value="/delete/{ID}")
      public String deleteService(@PathVariable("ID") final Integer ids) {
          System.out.println("hello");
          articleService.deleteService(ids);

          return "redirect:/articles";

      }

}

    td><a href="/articles/delete/${article.articleId}.html">delete</a></td> 

这是我的 web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<url-pattern>*.html</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

问题是

HTTP 状态 404 -/articles/delete/2.html

<小时/>

输入状态报告

消息/articles/delete/2.html

描述请求的资源不可用。

最佳答案

Http 404 错误的原因是找不到您的 http 请求的映射。从您的配置来看,您的 Controller 和请求映射似乎尚未配置。

您需要使用一些上下文配置来定义您的调度程序,如下所示:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

然后在 servlet-context.xml 中,您需要按如下方式定义 component-scan 来扫描注释驱动的 Controller :

<annotation-driven />
<context:component-scan base-package="net.roseindia.controller" />

关于java - springMVC,如何删除链接(href),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20256353/

相关文章:

java - hibernate 多对一急切获取不起作用

java - 在这种情况下我应该有两个支持 bean 吗

javascript - Spring boot自定义gson BEGIN_OBJECT但出现STRING错误

java - 错误无法找到 Java SE 运行时环境/从头开始

java - 将图像放在动态生成的 TextView 左侧的问题

java - Eclipse Spring Boot 构建路径包含重复条目

java - 如何获取所有源代码文件并检查是否已以编程方式在Eclipse中对其进行了修改

java - Spring Scheduler - 当存在循环依赖时,计划的方法不会在事务中启动

java - 如果我将 EJB 放入 EAR 文件中,我应该将实体类、单独的 jar 或 Web 应用程序放入其中吗?

java - 如何用java编写文件搜索程序?