gpt4 book ai didi

java - 主页RequestMapping。 Spring MVC

转载 作者:行者123 更新时间:2023-11-29 07:51:49 26 4
gpt4 key购买 nike

当我加载起始页时,我尝试从数据库中获取所有可用的组。 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 中设置了正确的 'value = "/"' 和名称 mav = new ModelAndView("");

这是我的包含所有设置的 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>

我应该如何在加载 JSP 页面时获取我需要的所有数据?

最佳答案

您已经在 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>

关于java - 主页RequestMapping。 Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20594575/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com