简体   繁体   English

springMVC,如何删除链接(href)

[英]springMVC ,how to delete a link (href)

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";

      }

}

may be here~~~~~~~~~~~~~~~~~~~~~~ 可能在这里~~~~~~~~~~~~~~~~~~~~~~

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

This problem just sticked me a lot.I think it might be the problem of href.Controller could not grasp the link of href 这个问题让我很困扰。我认为这可能是href的问题.Controller无法掌握href的链接

(second try)But It seems it also does not work like this (第二次尝试)但是似乎也不能这样工作

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> 

this is my web.xml 这是我的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>

Promble is 问题是

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


type Status report 类型状态报告

message /articles/delete/2.html 消息/articles/delete/2.html

description The requested resource is not available. 描述所请求的资源不可用。

Reason for Http 404 error is no mapping found for your http request. Http 404错误的原因是找不到您的http请求的映射。 Seems from your configurations that your controller and request mapping is not getting configured. 从您的配置中似乎未配置控制器和请求映射。

you need to define your dispatcher with some context configuration as follows: 您需要使用一些上下文配置来定义调度程序 ,如下所示:

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

and then in servlet-context.xml , you need to define component-scan as follows to scan your annotation driven controllers: 然后在servlet-context.xml ,需要定义组件扫描 ,如下所示以扫描注释驱动的控制器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM