简体   繁体   English

java问题:index.jsp文件无法显示到指定端口?

[英]java problem : index.jsp file can not display to specified port?

I am writing a very simple demo java spring website and here is my problem.我正在写一个非常简单的演示 java spring 网站,这是我的问题。

I put the index.jsp file in this folder我把index.jsp文件放在这个文件夹里

src/main/webapp/index.jsp

And the file is very simple而且文件很简单

I configured my file as the tutorial (changed all the port from 8080 to 8888), in the folder src/main/webapp/WEB-INF I made 2 files我将我的文件配置为教程(将所有端口从 8080 更改为 8888),在文件夹 src/main/webapp/WEB-INF 我制作了 2 个文件

demo-config-servlet.xml and web.xml, there were no error. demo-config-servlet.xml和web.xml,都没有报错。

Then I ran the file index.jsp, there was no error, as you can see in this picture然后我运行文件 index.jsp,没有错误,正如你在这张图片中看到的

当我运行文件 index.jsp

But when I ran DemoSpringMVC by Run As -> Run on Server -> (I choose Tomcat v10.0) I got this error但是当我通过 Run As -> Run on Server -> (我选择 Tomcat v10.0)运行 DemoSpringMVC 时,我得到了这个错误

我的错误

Here is my code这是我的代码

https://github.com/nguyencuc2586/Demo

Do you think it is because of the port 8888?你认为这是因为端口8888吗? Or the version of Tomcat?还是Tomcat的版本? Could you please give me some advices?你能给我一些建议吗? Thank you in advance.先感谢您。

Looked at your code on GitHub.在 GitHub 上查看了您的代码。 There are a couple of issues in web.xml . web.xml中有几个问题。

  • You need to tell the DispatcherServlet to load the context configuration (the one you defined in the demo-config-servlet.xml ) by specifying the contextConfigLocation init-param.您需要通过指定contextConfigLocation init-param 告诉DispatcherServlet加载上下文配置(您在demo-config-servlet.xml中定义的配置)。
  • Also map the dispatcher servlet correctly.还要正确映射调度程序 servlet。

Look at the updated web.xml below (added comments at the sections I added/modified):查看下面更新后的web.xml (在我添加/修改的部分添加注释):

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <servlet>
        <servlet-name>demo-config</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <!-- Mapped the servlet to '/' -->
    <servlet-mapping>
        <servlet-name>demo-config</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

EDIT: Added some explanation of the changes编辑:添加了对更改的一些解释

Spring Dispatcher servlet mapping needs to be a valid url-pattern. Spring Dispatcher servlet 映射需要是有效的 url 模式。 / in the pattern means that the servlet will be mapped to the 'root' /在模式中表示 servlet 将映射到“root”

<url-pattern>/<url-pattern>

In your case, this should take care of the issue.在您的情况下,这应该可以解决问题。 However, if for some reason the context still does not load, try specifying the context configuration to the servlet by using the <init-param> settings given below:但是,如果出于某种原因上下文仍然没有加载,请尝试使用下面给出的<init-param>设置为 servlet 指定上下文配置:

<!-- Added the context configuration below -->
<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/demo-config-servlet.xml</param-value>
</init-param>

Read about the configuration in the docs here - https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-servlet在此处阅读文档中的配置 - https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-servlet

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM