如何在SpringMVC应用程序中设置欢迎页而不是web inf文件夹?

sqxo8psd  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(356)

这是我的应用程序的目录结构,我突出显示了welcome.jsp文件(可以直接在web inf文件夹中找到)。

我想将此应用程序的默认页/欢迎页(尝试在服务器上运行应用程序时需要打开的页)保留在web inf/view文件夹中。我需要为此更改什么配置才能运行。请帮助,提前谢谢。
web.xml文件:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>subu</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>subu</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

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

</web-app>

调度器servlet,即。
subu-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:ctx="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd ">

    <ctx:annotation-config></ctx:annotation-config>
    <ctx:component-scan base-package="com.controller"></ctx:component-scan>

    <bean id="viewResolver"
      class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/view/"/>
    <property name="suffix" value=".jsp"/>
</bean>

</beans>

欢迎.jsp:

<html>
<h1>Welcome page !!!!!!!!</h1>
<body>

<form action="add">
    <input type="text" name="t1"><br>
    <input type="text" name="t2"><br>
    <input type="submit">
</form>
</body>
</html>

com.controller包下的addcontroller.java文件:

package com.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.service.AddService;

@Controller
public class AddController
{

    @RequestMapping("/add")
    public ModelAndView add(HttpServletRequest request,HttpServletResponse res)
    {
        int i=Integer.parseInt(request.getParameter("t1"));
        int j=Integer.parseInt(request.getParameter("t2"));

        AddService as=new AddService();
        int k=as.add(i, j);

        ModelAndView mv=new ModelAndView();//need to specify 2 things,view name and what data you want to pass

        mv.setViewName("display");
        mv.addObject("result",k);

        return mv;
    }

}
iqjalb3h

iqjalb3h1#

请添加 <mvc:view-controller path="/" view-name="yourviewPage"/> 应用程序配置文件。然后根目录将解析为yourviewpage视图。
您可以将路径值更改为welcomepath
然后添加

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

应用程序配置文件。这将把视图解析为 /WEB-INF/view/yourviewPage.jsp .

相关问题