當我嘗試讀取我彈簧控制器中的屬性文件時,我收到FileNotFoundException
。Spring MVC中的屬性文件的FileNotFoundError
這裏是顯示在何時何地出現日誌:
java.io.FileNotFoundException: src\main\webapp\WEB-INF\props\configFile.properties (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at com.webclaims.translator.Translator.readPropertiesFile(Translator.java:65)
at com.webclaims.translator.Translator.createEditablePage(Translator.java:45)
at com.webclaims.translator.controllers.TestController.editableWebpage(TestController.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
我的屬性文件的位置是src/main/webapp/WEB-INF/props/configFile.properties
這裏是有一個在日誌中顯示editableWebpage()
控制器:
@RequestMapping(value = "/edit")
public ModelAndView editableWebpage() throws IOException {
final String source = "http://localhost:8080/translator/test";
final String target = "src/main/webapp/WEB-INF/jsp/editable_webpage.jsp";
final String config = "src/main/webapp/WEB-INF/props/configFile.properties";
Translator t = new Translator();
t.createEditablePage(source, target, config);
return new ModelAndView("editable_webpage");
}
然後我們去我的Java類,它有一個以上的日誌中看到的createEditablePage()
:
@PropertySource(value = "configFile.properties")
public class Translator {
@Autowired
private Properties properties;
private static final Logger LOGGER = Logger.getLogger(Translator.class.getName());
public Translator() {
this.properties = new Properties();
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void createEditablePage(String source, String target, String config) {
File file = new File(target);
try {
Document doc = Jsoup.connect(source).get();
Elements elements = doc.select("*");
readPropertiesFile(config);
for(Element element : elements) {
if(!element.ownText().equals("")) {
String key = getKeyFromPropertiesFile(element.text().toString());
if(!key.equals("")) {
element.addClass(key);
element.attr("contentEditable", "true");
}
}
}
FileUtils.writeStringToFile(file, doc.outerHtml(), "UTF-8");
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "IOException has occured", e);
}
}
private void readPropertiesFile(String config) {
try {
File propsFile = new File(config);
FileInputStream inputStream = new FileInputStream(propsFile);
properties.load(inputStream);
inputStream.close();
} catch(FileNotFoundException e) {
LOGGER.log(Level.SEVERE, "FileNotFoundException has occured", e);
} catch(IOException e) {
LOGGER.log(Level.SEVERE, "IOException has occured", e);
}
}
以下是我在我的web.xml:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>translator</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.webclaims.*" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="propertiesFile" class="com.webclaims.translator.Translator">
<property name="properties" value="/WEB-INF/props/configFile.properties"></property>
</bean>
你有沒有試圖把'configFile.properties'資源文件夾? –
我該如何訪問它?是不是像'classpath:configFile.properties'? – cod3min3
是的。 classpath:configFile.properties – kuhajeyan