主页请求Map,Spring MVC

5w9g7ksd  于 2023-05-07  发布在  Spring
关注(0)|答案(3)|浏览(191)

当我加载起始页时,我尝试从数据库中获取所有可用的组。顺便说一句,我不能为主页设置正确的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 =“/"'和name 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页面时,我应该如何将所有需要的数据获取到JSP页面?

rbpvctlc

rbpvctlc1#

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

cig3rfwq2#

你必须在你的控制器中返回视图名。因为你用前缀/WEB-INF/jsp/和后缀.jsp配置了InternalResourceViewResolver,你应该返回index以获取/WEB-INF/jsp/index.jsp作为视图。
System. out. println(“index”);

vs3odd8k

vs3odd8k3#

您可以实现如下工作流程:
基本上,您的Web应用程序将查看欢迎页面,并且/redirect被触发,这是由控制器捕获的,并执行逻辑以从DB中获取所有可用的组并显示在启动页面中。
在web.xml中添加以下代码并将startup.jsp放入webapp目录。

<welcome-file-list>
    <welcome-file>startup.jsp</welcome-file>
</welcome-file-list>

startup.jsp中,添加以下代码:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<c:redirect url="/redirect"/>
</body>
</html>

最后,在Controller中,您可以添加以下代码:

@Controller
public class HomePageController {

    @RequestMapping(value = "/redirect", method = RequestMethod.GET)
    public String runMethod(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException {
       // add your logic to fetch all groups 
     return "results"; // return results.jsp where it has all the groups
    }
}

相关问题