简体   繁体   English

如何在 WEB-INF 文件夹以外的 spring MVC 应用程序中设置欢迎页面?

[英]How to set the welcome page in a spring MVC application other than WEB-INF folder?

Here is the directory structure of my application.I have highlighted the welcome.jsp file(which is available in WEB-INF folder directly).这是我的应用程序的目录结构。我已经突出显示了welcome.jsp 文件(可以直接在WEB-INF 文件夹中找到)。

在此处输入图片说明

I want to keep the default page/welcome page(page which need to open when try to run the app in server) for this application inside WEB-INF/view folder.What configuration i need to change for this sothat it can run .Please help !我想在 WEB-INF/view 文件夹中保留此应用程序的默认页面/欢迎页面(尝试在服务器中运行应用程序时需要打开的页面)。我需要为此更改什么配置才能运行。请帮助 ! ,thanks in advance. ,提前致谢。

web.xml:网页.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>

dispatcher servlet ie调度程序 servlet 即

subu-servlet.xml: 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>

welcome.jsp :欢迎.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>

AddController.java file under com.controller package:在 com.controller 包下添加Controller.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;
    }

}



  1. Please add <mvc:view-controller path="/" view-name="yourviewPage"/> to the application config file.请将<mvc:view-controller path="/" view-name="yourviewPage"/>到应用程序配置文件中。 Then the ROOT will resolve to the yourviewPage view.然后 ROOT 将解析为 yourviewPage 视图。

You can change the path value to the welcomepath您可以将路径值更改为welcomepath

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

to the applicationconfig file.到应用程序配置文件。 This will resolve the view to /WEB-INF/view/yourviewPage.jsp .这会将视图解析为/WEB-INF/view/yourviewPage.jsp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Spring MVC的Web应用程序的web-inf文件夹中写入txt文件 - Write a txt file in web-inf folder of a web application in Spring MVC spring mvc在WEB-INF文件夹中找不到jasper文件 - spring mvc cannot find the jasper file in WEB-INF folder WEB-INF 文件夹未在 Spring Boot 应用程序中创建? - WEB-INF folder is not creating in Spring Boot application? 在Spring MVC应用程序中的webcontent或web-inf上创建一个新文件 - Create a new file at webcontent or web-inf in a Spring MVC Application 来自WEB-INF文件夹的web.xml欢迎文件 - web.xml welcome file from WEB-INF folder 如何访问WEB-INF下文件夹内的jsp页面? - How to access a jsp page inside a folder under WEB-INF? 如何在spring中将属性设置为WEB-INF中的路径名? - How to set a property in spring to a pathname inside of WEB-INF? 如何使用javascript / ajax调用在Spring MVC项目中的/ WEB-INF /中的单独文件夹下访问jsp文件? - How to Access the jsp files under a separate folder in /WEB-INF/ in a Spring MVC project using javascript/ajax calls? Jetty升级后,Spring MVC在WEB-INF文件夹中找不到jspx文件 - Spring MVC can't find jspx-files in WEB-INF folder after jetty upgrade 访问 WEB-INF 文件夹中的网站背景和其他装饰图像 - Accessing images of website background and other decorations in WEB-INF folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM