如何在Spring MVC应用程序中设置欢迎页面,而不是在WEB-INF文件夹中?

huangapple go评论83阅读模式
英文:

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

问题

这是我的应用程序的目录结构。我已经突出显示了 welcome.jsp 文件(直接位于 WEB-INF 文件夹中)。

如何在Spring MVC应用程序中设置欢迎页面,而不是在WEB-INF文件夹中?

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>

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 文件:

<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();//需要指定两个内容,视图名称和要传递的数据
	
		mv.setViewName("display");
		mv.addObject("result",k);
		
		return mv;
	}

}
英文:

Here is the directory structure of my application.I have highlighted the welcome.jsp file(which is available in WEB-INF folder directly).

如何在Spring MVC应用程序中设置欢迎页面,而不是在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 ! ,thanks in advance.

web.xml:

&lt;!DOCTYPE web-app PUBLIC
 &quot;-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quot;
 &quot;http://java.sun.com/dtd/web-app_2_3.dtd&quot; &gt;

&lt;web-app&gt;
  &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
  
  &lt;servlet&gt;
    &lt;servlet-name&gt;subu&lt;/servlet-name&gt;
    &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;subu&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  
  &lt;welcome-file-list&gt;
    &lt;welcome-file&gt;welcome.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  
&lt;/web-app&gt;

dispatcher servlet i.e.

subu-servlet.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
	xmlns:ctx=&quot;http://www.springframework.org/schema/context&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
	xsi:schemaLocation=&quot;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 &quot;&gt;
	
	
	&lt;ctx:annotation-config&gt;&lt;/ctx:annotation-config&gt;
	&lt;ctx:component-scan base-package=&quot;com.controller&quot;&gt;&lt;/ctx:component-scan&gt;
	
     
     
     
    &lt;bean id=&quot;viewResolver&quot;
      class=&quot;org.springframework.web.servlet.view.UrlBasedViewResolver&quot;&gt;
    &lt;property name=&quot;viewClass&quot; value=&quot;org.springframework.web.servlet.view.JstlView&quot;/&gt;
    &lt;property name=&quot;prefix&quot; value=&quot;/WEB-INF/view/&quot;/&gt;
    &lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot;/&gt;
&lt;/bean&gt;
	
	
&lt;/beans&gt;

welcome.jsp :

&lt;html&gt;
&lt;h1&gt;Welcome page !!!!!!!!&lt;/h1&gt;
&lt;body&gt;


&lt;form action=&quot;add&quot;&gt;
	&lt;input type=&quot;text&quot; name=&quot;t1&quot;&gt;&lt;br&gt;
	&lt;input type=&quot;text&quot; name=&quot;t2&quot;&gt;&lt;br&gt;
	&lt;input type=&quot;submit&quot;&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

AddController.java file under com.controller package:

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(&quot;/add&quot;)
	public ModelAndView add(HttpServletRequest request,HttpServletResponse res)
	{
		int i=Integer.parseInt(request.getParameter(&quot;t1&quot;));
		int j=Integer.parseInt(request.getParameter(&quot;t2&quot;));
		
		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(&quot;display&quot;);
		mv.addObject(&quot;result&quot;,k);
		
		return mv;
	}

}



答案1

得分: 2

  1. 请将以下代码添加到应用配置文件中:&lt;mvc:view-controller path=&quot;/&quot; view-name=&quot;yourviewPage&quot;/&gt;。这样,根路径将解析为 yourviewPage 视图。

您可以将 path 的值更改为 welcomepath

  1. 然后,将以下代码添加到应用配置文件中:
&lt;bean class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;
    &lt;property name=&quot;prefix&quot; value=&quot;/WEB-INF/views/&quot;/&gt;
    &lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot;/&gt;
&lt;/bean&gt;

这将会将视图解析为 /WEB-INF/views/yourviewPage.jsp

英文:
  1. Please add &lt;mvc:view-controller path=&quot;/&quot; view-name=&quot;yourviewPage&quot;/&gt; to the application config file. Then the ROOT will resolve to the yourviewPage view.

You can change the path value to the welcomepath

  1. Then add
&lt;bean class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;
    &lt;property name=&quot;prefix&quot; value=&quot;/WEB-INF/views/&quot;/&gt;
    &lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot;/&gt;
&lt;/bean&gt;

to the applicationconfig file. This will resolve the view to /WEB-INF/view/yourviewPage.jsp.

huangapple
  • 本文由 发表于 2020年9月4日 14:33:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63735998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定