【问题标题】:Cannot access to resources when I add method = RequestMethod.POST添加方法 = RequestMethod.POST 时无法访问资源
【发布时间】:2016-01-10 23:57:18
【问题描述】:

在我的项目中使用 css 和 js 等访问资源文件夹时遇到问题。 当我使用方法 = RequestMethod.POST 添加@RequestMapping 时,我无法访问资源。在一切正常之前。

我的结构:

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>medicine</display-name>
 <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>/</url-pattern>
 </servlet-mapping>

</web-app>

调度程序-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

<context:component-scan base-package="pl.tomo" /> 
<jpa:repositories base-package="pl.tomo.repository"/>


<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />


<tx:annotation-driven/>
</beans>

我的控制器

package pl.tomo.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import pl.tomo.entity.Lek;
import pl.tomo.entity.Medicament;
import pl.tomo.service.LekService;
import pl.tomo.service.MedicamentService;

@Controller
public class IndexController {

@Autowired
private MedicamentService medicamentService;

@Autowired
private LekService lekService;

private List<Lek> listLek = new ArrayList<Lek>();

@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(Model model)
{
    listLek = lekService.findAll();
    model.addAttribute("listLek", listLek);
    model.addAttribute("lek", new Lek());
    return "index";
}

@RequestMapping(name = "/index", method = RequestMethod.POST)
public String add(@ModelAttribute("lek") Lek lek)
{
    try {
        lek.setDateOpen(new SimpleDateFormat("yyyy-MM-dd").parse(lek.getDateStringOpen()));
        lek.setDateExpiration(new SimpleDateFormat("yyyy-MM-dd").parse(lek.getDateStringExpiration()));
        lek.setDateEnd(new SimpleDateFormat("yyyy-MM-dd").parse(lek.getDateStringEnd()));

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Medicament medicament = medicamentService.findOne(lek.getLiczba());
    lek.setMedicament(medicament);
    lekService.save(lek);
    return "redirect:/index";
}

}

index.jsp 中的表单

<form:form method="POST" commandName="lek" class="form-inline">
    <form:input path="dateStringOpen" cssClass="form-control" />
    <form:input path="dateStringExpiration" cssClass="form-control" />
    <form:input path="dateStringEnd" cssClass="form-control" />
    <input type="submit" value="Dodaj lek" Class="btn btn-default" />
</form:form>

我想去localhost:8080/resources/core/css/bootstrap.min.css,我有

HTTP ERROR 405

Problem accessing /resources/core/css/bootstrap.min.css. Reason:

Request method 'GET' not supported

当我删除时

 @RequestMapping(name = "/index", method = RequestMethod.POST)
 public String add(@ModelAttribute("lek") Lek lek)
    {
    try {
        lek.setDateOpen(new SimpleDateFormat("yyyy-MM-dd").parse(lek.getDateStringOpen()));
        lek.setDateExpiration(new SimpleDateFormat("yyyy-MM-dd").parse(lek.getDateStringExpiration()));
        lek.setDateEnd(new SimpleDateFormat("yyyy-MM-dd").parse(lek.getDateStringEnd()));

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Medicament medicament = medicamentService.findOne(lek.getLiczba());
    lek.setMedicament(medicament);
    lekService.save(lek);
    return "redirect:/index";
}

我可以访问文件localhost:8080/resources/core/css/bootstrap.min.css

你能帮帮我吗?

【问题讨论】:

    标签: java spring model-view-controller


    【解决方案1】:

    我以不同方式配置了对资源的访问。对于初学者,我只使用注释。从那里,我添加了一个资源处理程序:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
    

    【讨论】:

      【解决方案2】:

      我已经从

      更改了 servlet 映射
      <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>/</url-pattern>
      </servlet-mapping>
      

      <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
          <url-pattern>*.html</url-pattern>
          <url-pattern>*.htm</url-pattern>
          <url-pattern>*.json</url-pattern>
          <url-pattern>*.xml</url-pattern>
      </servlet-mapping>
      

      它可以工作

      【讨论】:

        猜你喜欢
        • 2012-06-22
        • 2018-12-08
        • 2012-12-20
        • 1970-01-01
        • 1970-01-01
        • 2012-11-05
        • 2015-07-17
        • 2019-01-02
        • 1970-01-01
        相关资源
        最近更新 更多