0

您好我正在尝试将引导程序添加到在 Servlet 3.0 容器(Websphere 8.5)上运行的 Spring-MVC 5 应用程序中。

在将引导程序添加到我的应用程序之前。我的应用程序配置和测试控制器如下:

  • Web.xml

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

  • 调度程序-servlet.xml

    <context:component-scan base-package="com.lagatahk.controller" />
    
    <context:annotation-config /> 
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

  • helloworldController.java

    @Controller
    public class HelloWorldController {
        String message = "Welcome to Spring MVC5!";
         
        @RequestMapping("/index")
        public ModelAndView showIndex(
                @RequestParam(value = "name", required = false, defaultValue = "World") String name) {
            System.out.println("in /index controller");
            ModelAndView mv = new ModelAndView("index");
            mv.addObject("message", message);
            mv.addObject("name", "UserName");
            return mv;
        }

原图在这里

通过这个配置,我在 pom.xml 中添加了 bootstrap 和 jquery 的 webjar:

  • pom.xml

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>5.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.6.0</version>
        </dependency>

  • 在我的 dispatcher-servlet.xml 中添加了这一行

<mvc:resources mapping="/webjars/**" location="/webjars/" />

  • 参考引导样式表更改视图 index.jsp

    <!DOCTYPE html>
    <html>
    <head>
    <script src="/webjars/jquery/3.6.0/jquery.min.js"></script>
    <script src="/webjars/bootstrap/5.1.0/js/bootstrap.min.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet"
        href="/webjars/bootstrap/5.1.0/css/bootstrap.min.css" />
    <title>Spring 5 MVC - Index Page</title>
    </head>
    <body>
        <center>
            <h1>Welcome to this Spring 5 MVC Sample Project</h1>
            <p>This is a Spring 5 MVC Example App using apache maven webapp
                archieType. <br/>
            <ol>
                <li><a href="https://www.google.com">First
                        Reference</a></li>
            </ol>
            </p>
        </center>
        <div class="container">
            <br />
            <div class="alert alert-success">
                <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
                <strong>Hi ${name}!</strong> It is working as we expected. Your
                message is ${message}
            </div>
        </div>
    </body>
    </html>

但是,启动我的应用程序后,我原来的控制器和视图似乎不再工作了。在应用程序服务器日志中:


[8/15/21 22:57:48:062 CST] 0000009f PageNotFound  W 
org.springframework.web.servlet.DispatcherServlet noHandlerFound No mapping found for HTTP 
request with URI [/springlearnweb/index] in DispatcherServlet with name 'dispatcher'

我错过了任何配置吗?请帮忙 !

4

0 回答 0