当我加载起始页面时,我尝试从db获取所有可用的组。 Btu我无法为主页设置正确的RequestMapping,当我加载主页时,它会将任何数据加载到JSP页面。我尝试将其设置为
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView newStudentForm() {
ModelAndView mav = new ModelAndView("");
mav.getModelMap().put("allGroups", gss.selectAllGroups()); // add all available groups
return mav;
}
但我不确定我在ModelAndView mav = new ModelAndView(“”)中设置了正确的'value =“/”'和名字;
这是我的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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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">
<display-name>IRSystem</display-name>
<!-- The start page -->
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>IRSystemServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/IRSystemServlet-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>IRSystemServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
IRSystemServlet-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:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- Set packages to scan for necessary components-->
<context:component-scan base-package="org.irs.controllers"/>
<context:component-scan base-package="org.irs.service"/>
<context:component-scan base-package="org.irs.dao"/>
<context:component-scan base-package="org.irs.entitis"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- JDBC DataSource -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:VLAD-PC/@localhost:1521:orcl"/>
<property name="username" value="VLAD"/>
<property name="password" value="admin"/>
</bean>
<!-- Hibernate session -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.OracleDialect</value>
</property>
<property name="packagesToScan" value="org.irs.entities"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- set response encoding UTF-8 -->
<bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<array>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
</bean>
</array>
</property>
</bean>
<mvc:annotation-driven/>
<!-- Hibernate transaction -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
当我加载它时,我该怎么做才能获得我需要的所有数据?
答案 0 :(得分:4)
您已在ModelAndView对象中设置了类似此内容的主页
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView newStudentForm() {
ModelAndView mav = new ModelAndView("index");
mav.getModelMap().put("allGroups", gss.selectAllGroups()); // add all available groups
return mav;
}
当您点击 / 时,您不需要拥有欢迎页面,它会调用上述方法。 删除此代码
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>