1

在登录屏幕中。我想通过连接数据库来检查用户。但我必须在 Java MVC 架构中做到这一点。

首先,在 login.js 中,我想访问 Contact 控制器中的 login.action,最后,将 json 数组返回为成功或失败。然后我去服务和DAO。

另外,我是 java mvc 架构的新程序员,如何在那里使用它?

我的接触控制器代码如下:

package com.loiane.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loiane.model.Contact;
import com.loiane.service.ContactService;


@Controller
public class ContactController  {

private ContactService contactService;

@RequestMapping(value="/contact/view.action")
public @ResponseBody Map<String,? extends Object> view() throws Exception {

    try{

        List<Contact> contacts = contactService.getContactList();

        return getMap(contacts);

    } catch (Exception e) {

        return getModelMapError("Error retrieving Contacts from database.");
    }
}

@RequestMapping(value="/contact/login.action")
public @ResponseBody Boolean login(@RequestParam Object data) throws Exception {

    try{

        Boolean res = contactService.login(data);
    return res;
    } catch (Exception e) {


    }
    return false;
}

@RequestMapping(value="/contact/create.action")
public @ResponseBody Map<String,? extends Object> create(@RequestParam Object data) throws Exception {

    try{

        List<Contact> contacts = contactService.create(data);

        return getMap(contacts);

    } catch (Exception e) {

        return getModelMapError("Error trying to create contact.");
    }
}

@RequestMapping(value="/contact/update.action")
public @ResponseBody Map<String,? extends Object> update(@RequestParam Object data) throws Exception {
    try{

        List<Contact> contacts = contactService.update(data);

        return getMap(contacts);

    } catch (Exception e) {

        return getModelMapError("Error trying to update contact.");
    }
}

@RequestMapping(value="/contact/delete.action")
public @ResponseBody Map<String,? extends Object> delete(@RequestParam Object data) throws Exception {

    try{

        contactService.delete(data);

        Map<String,Object> modelMap = new HashMap<String,Object>(3);
        modelMap.put("success", true);

        return modelMap;

    } catch (Exception e) {

        return getModelMapError("Error trying to delete contact.");
    }
}

/**
 * Generates modelMap to return in the modelAndView
 * @param contacts
 * @return
 */
private Map<String,Object> getMap(List<Contact> contacts){

    Map<String,Object> modelMap = new HashMap<String,Object>(3);
    modelMap.put("total", contacts.size());
    modelMap.put("data", contacts);
    modelMap.put("success", true);

    return modelMap;
}

/**
 * Generates modelMap to return in the modelAndView in case
 * of exception
 * @param msg message
 * @return
 */
private Map<String,Object> getModelMapError(String msg){

    Map<String,Object> modelMap = new HashMap<String,Object>(2);
    modelMap.put("message", msg);
    modelMap.put("success", false);

    return modelMap;
} 


@Autowired
public void setContactService(ContactService contactService) {
    this.contactService = contactService;
}

}

我的 Login.js 代码如下:

Ext.onReady(function(){
Ext.QuickTips.init();

// Create a variable to hold our EXT Form Panel. 
// Assign various config options as seen.    
var login = new Ext.FormPanel({ 
    labelWidth:80,
    url:'Login.jsp', 
    frame:true, 
    title:'Lütfen Giriş Yapınız.', 
    defaultType:'textfield',
monitorValid:true,
// Specific attributes for the text fields for username / password. 
// The "name" attribute defines the name of variables sent to the server.
    items:[{ 
            fieldLabel:'Kullanıcı Adı', 
            name:'loginUsername', 
            allowBlank:false 
        },{ 
            fieldLabel:'Parola', 
            name:'loginPassword', 
            inputType:'password', 
            allowBlank:false 
        }],

// All the magic happens after the user clicks the button     
    buttons:[{ 
            text:'Login',
            formBind: true,  
            // Function that fires when user clicks the button 
            handler:function(){ 
                login.getForm().submit({ 
                    method:'POST', 
                    waitTitle:'Connecting', 
                    waitMsg:'Sending data...',

        // Functions that fire (success or failure) when the server responds. 
        // The one that executes is determined by the 
        // response that comes from login.asp as seen below. The server would 
        // actually respond with valid JSON, 
        // something like: response.write "{ success: true}" or 
        // response.write "{ success: false, errors: { reason: 'Login failed. Try again.' }}" 
        // depending on the logic contained within your server script.
        // If a success occurs, the user is notified with an alert messagebox, 
        // and when they click "OK", they are redirected to whatever page
        // you define as redirect. 

                    success:function(){ 
                        Ext.Msg.alert('Status', 'Login Successful!', function(btn, text){
               if (btn == 'ok'){
                            var redirect = 'home.jsp'; 
                            window.location = redirect;
                               }
                });
                    },

        // Failure function, see comment above re: success and failure. 
        // You can see here, if login fails, it throws a messagebox
        // at the user telling him / her as much.  

                    failure:function(form, action){ 
                        if(action.failureType == 'server'){ 
                            obj = Ext.util.JSON.decode(action.response.responseText); 
                            Ext.Msg.alert('Login Failed!', obj.errors.reason); 
                        }else{ 
                            Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + action.response.responseText); 
                        } 
                        var redirect = 'home.jsp'; 
                        window.location = redirect;
                    } 
                }); 
            } 
        }] 
});


// This just creates a window to wrap the login form. 
// The login object is passed to the items collection.       
var win = new Ext.Window({
    layout:'fit',
    width:300,
    height:150,
    closable: false,
    resizable: false,
    plain: true,
    border: false,
    items: [login]
});
win.show();

});

最后:Login.jsp 代码如下:(另外,我不知道我必须在 jsp 中做什么。这是必要的,我在 jsp 中编码什么)。

<%@page import="com.loiane.web.ContactController"%>
<%@ page language="java" pageEncoding="UTF-8"%>
 <%
String result;
    String loginUsername = request.getParameter("loginUsername");
    String loginPassword = request.getParameter("loginPassword");

 if ((null != loginUsername && loginUsername.length() > 0) && (null != loginPassword && loginPassword.length() > 0)) {

    if (loginUsername.equals("f"))
        //check

        result = "{success:true}";
    else
        result = "{success:false,errors:{reason:'Login failed.Try again'}}";

} else {
    result = "{success:false,errors:{reason:'Login failed.Try again'}}";
}

%>

4

0 回答 0