简体   繁体   English

如何使用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?

My Project Structure is this: 我的项目结构是这样的:

在此处输入图片说明

The body of my LoginPage is the following 我的LoginPage的正文如下

<body>
<P id="errors"></P>
<p>Login Details</p>
   <div id = "page"></div>

    <table>
        <tr>
            <td> Login ID :</td>
            <td><input type="number" id="loginid" min="1" max="200" style="width:169px;"/></td>
        </tr>
        <tr>
            <td> Passowrd :</td>
            <td><input type="password" id="password"/></td>
        <tr>

    </table>
    <button id="loginB" onclick="login()">submit</button>
</body>

After Successful Login I would like to move from loginPage.jsp to home.jsp using JavaScript or Ajax. 成功登录后,我想使用JavaScript或Ajax从loginPage.jsp移到home.jsp。 My code is 我的代码是

function login()
{

var name = document.getElementById("loginid").value;
var password = document.getElementById("password").value;
var url="/loginPage?name="+name+"&password="+password;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     var responsetext=xhttp.responseText;
     var parsedresult=JSON.parse(responsetext);
     if(parsedresult.success==true)
         {
         window.location="home.jsp";
         }
     else
         {
         document.getElementById("errors").innerHTML=parsedresult.message;
         }
         }

  };      

  xhttp.open("POST",url,true);
  xhttp.send();
}

It wouldn't go to home.jsp and give a 404 Error. 它不会转到home.jsp并给出404错误。 How to configure it to move to home.jsp ? 如何配置它以移到home.jsp?

My Login API is the following. 我的登录API如下。 The LoginModel has hardcoded username and password that is used for validation. LoginModel具有用于验证的硬编码用户名和密码。 The loginmodel.validate() would return a string "false" if the credentials are incorrect. 如果凭据不正确,loginmodel.validate()将返回字符串“ false”。

    @Controller
public class LoginController {

    LoginModel loginmodel = new LoginModel();
     @RequestMapping(value="/")
      public ModelAndView redirect(HttpServletRequest request, HttpServletResponse response) throws IOException
      {
          return new ModelAndView("loginPage");
      }

     @RequestMapping(value="/loginPage",method=RequestMethod.POST)
     public void login(HttpServletRequest request, HttpServletResponse response)
              throws IOException {

         String sessionName = loginmodel.validate(request.getParameter("name"), request.getParameter("password"));
         if(!sessionName.equals("false"))
         {
             HttpSession session = request.getSession();
             session.setAttribute("name", sessionName);
             JSONObject result= new JSONObject();
             result.put("success", true);            
             response.setContentType("application/json");
             response.getWriter().print(result);
         }
         else
         {    JSONObject result= new JSONObject();
              result.put("success", false);
              result.put("message", "invalid credentials");
             response.setContentType("application/json");
             response.getWriter().print(result);
         }

     }
     @RequestMapping(value="/logout")
     public String logout(HttpServletRequest request, HttpServletResponse response) throws IOException
      {  
          request.getSession().invalidate();
          return "loginPage";
      }

}

Please write one method in controller which returns home.jsp page like below : 请在控制器中编写一种方法,该方法将返回home.jsp页面,如下所示:

@RequestMapping(value="/home")
public String logout(HttpServletRequest request, HttpServletResponse response) throws IOException
{
    return "home";
}

AND then from your jsp page : 然后从您的jsp页面:

window.location=" contextPath /home"; window.location =“ contextPath / home”;

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

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