我的Web应用程序使用从Tomcat的/ webapps /文件夹开始的根路径作为应用程序根目录而不是WEB-INF文件夹。谁能告诉我配置错误的地方?
难度:它很混乱,因为转到索引页面工作正常。令人困惑的是RequestMapping工作正常,我可以看到Hibernate进行SQL查询,这意味着自动服务的服务(和存储库)被称为OK。
以下是我的配置文件:
的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" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>city-http</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/city-http-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>city-http</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
城市-HTTP-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:integ="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
<context:annotation-config />
<context:component-scan base-package="nikzgs.city.app" />
<!-- Maps a logical view name to a physical resource -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- setting maximum upload size -->
<property name="maxUploadSize" value="1900000" />
</bean>
<mvc:annotation-driven />
<!-- -->
<bean id="jdbcPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath:persistence.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="dataSource"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager"/>
<property name="persistenceUnitName" value="entityManager"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
IndexController工作正常:
package nikzgs.city.app.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/")
public String index() {
return "index";
}
}
/ WEB-INF / jsp /文件夹下的index.jsp显示OK:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index page</title>
</head>
<body>
<h3>Welcome!</h3>
<a href="${contextPath}/city/viewList">View cities</a>
<br />
<a href="/city/viewList">View cities without context path</a>
<br />
</body>
</html>
CityController:
package nikzgs.city.app.controller;
import java.util.List;
import nikzgs.city.app.entity.City;
import nikzgs.city.app.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/city")
public class CityController {
@Autowired
private CityService cityService;
@RequestMapping(value = "/viewList", method = RequestMethod.GET)
public String cityViewForm(Model model) {
return prepareCityListForReview(model);
}
@RequestMapping(value = "/addNewCity", method = RequestMethod.POST)
public String addNewCity(@ModelAttribute("newCity") City newCity, Model model) {
cityService.addCity(newCity);
return prepareCityListForReview(model);
}
@RequestMapping(value = {"/editCity","/modifyCity"}, method = RequestMethod.GET)
public String viewEditCityForm(@ModelAttribute("cityUnderEdit") City cityUnderEdit, @RequestParam("id")Long id, Model model) {
if (null != id) {
City cityToEdit = cityService.getCityById(id);
model.addAttribute("cityUnderEdit", cityToEdit);
}
return "city/cityEdit";
}
@RequestMapping(value = "/updateCity", method = RequestMethod.POST)
public String updateCity(@ModelAttribute("cityUnderEdit") City cityUnderEdit, Model model) {
cityService.updateCity(cityUnderEdit);
return prepareCityListForReview(model);
}
@RequestMapping(value = "/deleteCity", method = RequestMethod.GET)
public String deleteCity(@RequestParam("id")Long id, Model model) {
if (null != id) {
cityService.deleteCityById(id);
}
return prepareCityListForReview(model);
}
private String prepareCityListForReview(Model model) {
List<City> cities = cityService.getAllCities();
model.addAttribute("cities", cities);
model.addAttribute("newCity", new City());
return "city/cityList";
}
}
此外,2个包含网址和文件夹结构的屏幕截图,我相信它们会有所帮助
索引确定:
答案 0 :(得分:2)
变化
<property name="prefix" value="WEB-INF/jsp/"/>
到
<property name="prefix" value="/WEB-INF/jsp/"/>
还使用JSTL核心代码库<c:url value='/some-path'/>
输出正确的网址