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