添加method = RequestMethod.POST时无法访问资源

时间:2016-01-10 23:57:18

标签: java spring model-view-controller

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

我的结构:

enter image description here

的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

你能帮助我吗?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我已经从

更改了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>

并且有效