无法在Spring MVC中查看欢迎页面

时间:2018-08-04 12:31:52

标签: java spring jsp servlets model-view-controller

这是我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.gopal.springmvc</groupId>
<artifactId>springmvctest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springmvctest Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.0.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.4</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
</dependencies>
<build>
    <finalName>springmvctest</finalName>
</build>
</project>

这是我的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>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>
 </web-app>

这是我的dispatcher-servlet.xml文件:

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p" 
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

这是我的控制者:

  @Controller
  public class RegistrationLoginController {

  private static final Logger logger =    
  Logger.getLogger(RegistrationLoginController.class);

@RequestMapping(value="/", method=RequestMethod.GET)
public String welcome() {
    if(logger.isDebugEnabled()){
        logger.debug("getWelcome is executed!");
    }
    System.out.println("Helloooooooo<<<<<");
    //@RequestParam
    return "welcome";
}

@RequestMapping(value="/login", method=RequestMethod.POST)
public ModelAndView loginForm() {
    //@RequestParam
    return null;
}

}

这是我的welcome.jsp文件,该文件位于/ WEB-INF / jsp /文件夹中:

 <!DOCTYPE html>
  <html>
  <head>

  </head>
    <body>

     <h2>Login Form</h2>



      </body>
   </html>

当我使用eclipse在tomcat服务器中运行此应用程序时,无法找到welcome.jsp页面。我可以在控制器中看到控制台打印消息和日志消息。这意味着请求正在通过RegistrationLoginController的welcome()方法传递。我的问题是我无法获得welcome.jsp页面。请帮助我找到问题。

谢谢。

1 个答案:

答案 0 :(得分:0)

尝试:

using System;
using System.Runtime.InteropServices;

public class LowLevelMouseHook {
    #region Win32 API
    private const int WH_MOUSE_LL = 14;
    private const ulong WM_RBUTTONDOWN = 0x0204;

    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    private delegate long LowLevelMouseProc (int nCode, UIntPtr wParam, IntPtr lParam);

    private struct POINT {
        long x;
        long y;
    }

    private struct MSLLHOOKSTRUCT {
        POINT pt;
        int mouseData;
        int flags;
        int time;
        UIntPtr dwExtraInfo;
    }

    [DllImport("User32.dll", EntryPoint = "CallNextHookEx", SetLastError = true)]
    private static extern long CallNextHookEx (IntPtr hHook, int nCode, UIntPtr wParam, IntPtr lParam);

    [DllImport("Kernel32.dll", EntryPoint = "GetLastError", SetLastError = true)]
    private static extern uint GetLastError ();

    [DllImport("Kernel32.dll", EntryPoint = "LoadLibraryW", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern IntPtr LoadLibrary (string lpFileName);

    [DllImport("User32.dll", EntryPoint = "SetWindowsHookExW", SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx (int idHook, Delegate lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("User32.dll", EntryPoint = "UnhookWindowsHookEx", SetLastError = true)]
    private static extern byte UnhookWindowsHookEx (IntPtr hHook);
    #endregion

    // There shall only be one instance of this class
    private static LowLevelMouseHook instance = null;

    private IntPtr hHook;
    private LowLevelMouseProc hProc; // If I don't store the delegate, I get some CallbackOnCollectedDelegate exception

    private LowLevelMouseHook () {
        this.hProc = new LowLevelMouseProc(MouseProc);
        this.hHook = SetWindowsHookEx(WH_MOUSE_LL, this.hProc, LoadLibrary("User32.dll"), 0);
    }

    ~LowLevelMouseHook () {
        //
        // PROBLEM:
        // UnhookWindowsHookEx always returns 0 and GetLastError returns ERROR_INVALID_HOOK_HANDLE (1404)
        // even though this.hHook is still the same value SetWindowsHookEx returned.
        //
        if (UnhookWindowsHookEx(this.hHook) == 0)
            System.Windows.Forms.MessageBox.Show($"Error {GetLastError()} unhooking WH_MOUSE_LL", "UnhookWindowsHookEx Error");

        LowLevelMouseHook.instance = null;
    }

    // Create new LowLevelMouseHook instance.
    // This is called during the Startup-Event of Application.
    public static void Init () {
        LowLevelMouseHook.instance = new LowLevelMouseHook();
    }

    private static long MouseProc (int nCode, UIntPtr wParam, IntPtr lParam) {
        if ((ulong)wParam == WM_RBUTTONDOWN) {
            ; // Things to do; that works fine
        }

        return CallNextHookEx(LowLevelMouseHook.instance.hHook, nCode, wParam, lParam);
    }
}

要获取路径,请在您的IDE中右键单击文件“ welcome.jsp”,然后选择“复制路径”(对于IntelliJ,则为 Ctrl + Shift + C )。

然后像上面的示例一样将其粘贴到您的代码中。

您的控制器方法应如下所示:

#include <stdio.h>
#include <Windows.h>

FILE *file;
HHOOK hHook;

LRESULT CALLBACK LowLevelMouseProc_Test (int nCode, WPARAM wParam, LPARAM lParam) {
    PMSLLHOOKSTRUCT pHookStruct = (PMSLLHOOKSTRUCT)lParam;
    fprintf(file, "MouseProc_Test(nCode = %i, wParam = %lu, lParam->pt = (%i, %i))\n", nCode, wParam, pHookStruct->pt.x, pHookStruct->pt.y);
    return CallNextHookEx(hHook, nCode, wParam, lParam);
}

VOID CALLBACK TimerProc_Test (HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) {
    PostQuitMessage(0);
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nShow) {
    fopen_s(&file, "wm_mouse_ll.txt", "w");
    hHook = SetWindowsHookExW(WH_MOUSE_LL, LowLevelMouseProc_Test, LoadLibrary("User32.dll"), 0);

    if (hHook != 0) {
        fprintf(file, "SetWindowsHookExW Success (hHook = 0x%p)\n", hHook);

        SetTimer(NULL, 0, 5000, TimerProc_Test);

        MSG msg = {0};
        while (GetMessageW(&msg, NULL, 0, 0) != 0) {
            TranslateMessage(&msg);
            DispatchMessageW(&msg);
        }

        if (UnhookWindowsHookEx(hHook) != 0)
            fputs("UnhookWindowsHookEx Success\n", file);
        else
            fprintf(file, "UnhookWindowsHookEx Error %u\n", GetLastError());
    } else {
        fprintf(file, "SetWindowsHookExW Error %u\n", GetLastError());
    }

    fclose(file);
    return 0;
}