struts2.0+spring2.0+hibernate3.1 login and additions and deletions to the full demo example in serial ... Three

Source: Internet
Author: User

Well, the nonsense is not much to say, then this morning our example went on to finish.
Six, we began to write the spring layer:
1, the service Layer interface Iusersservice.java package org.login.service;

Import java.util.List;

Import Org.login.vo.Users;

@SuppressWarnings ("Unchecked")
Public interface Iusersservice {

public boolean checklogin (Users user);

public boolean saveuser (Users user);

public Boolean deleteuser (Integer ID);

public boolean updateUser (Users user);

Public List < Users > findallusers ();

Public Users Getuserbyid (Integer ID);
}
2. Service Interface Implementation layer: Usersservice.java
Package Org.login.service.Impl;

Import java.util.List;

Import Org.login.dao.IUsersDAO;
Import Org.login.service.IUsersService;
Import Org.login.vo.Users;

@SuppressWarnings ("Unchecked")
public class Usersservice implements Iusersservice {

Private Iusersdao Userdao = null;

Public Iusersdao Getuserdao () {
return Userdao;
}

public void Setuserdao (Iusersdao Userdao) {
this. Userdao = Userdao;
}

@Override
public boolean checklogin (Users user) {

return Userdao.checklogin (user);
}

@Override
public Boolean deleteuser (Integer ID) {

return Userdao.deleteuser (ID);
}

@Override
public boolean saveuser (Users user) {

return Userdao.saveuser (user);
}

@Override
public boolean updateUser (Users user) {

return Userdao.updateuser (user);
}

@Override
Public List < Users > findallusers () {

return Userdao.findallusers ();
}

@Override
Public Users Getuserbyid (Integer ID) {

return Userdao.getuserbyid (ID);
}

}

Seven, now we start writing the struts layer:
1, write the Action:Login.java of the struts layer
Package org.login.actions;

Import java.util.List;

Import Javax.servlet.http.HttpServletRequest;

Import Org.apache.struts2.interceptor.ServletRequestAware;
Import Org.login.service.IUsersService;
Import Org.login.vo.Users;

Import Com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings ({"Serial", "Unchecked"})
public class Login extends Actionsupport implements Servletrequestaware {

Private static final String update = "Update";

Private HttpServletRequest request = null;

Private Iusersservice userservice = null;

Private String id = null;
Private String name = NULL;
Private String password = null;

Public String GetName () {
return name;
}

public void SetName (String name) {
this. Name = name;
}

Public String GetPassword () {
return password;
}

public void SetPassword (String password) {
this. Password = password;
}

Public String getId () {
return ID;
}

public void SetId (String id) {
this. id = ID;
}

Public Iusersservice Getuserservice () {
return userservice;
}

public void Setuserservice (Iusersservice userservice) {
this. UserService = UserService;
}

@Override
Public String Execute () throws Exception {
Users user = new users ();
User.setusername (name);
User.setpassword (password);
if (Getuserservice (). Checklogin (user)) {
return SUCCESS;
}
return INPUT;
}

Public String Save () throws Exception {
Users user = new users ();
User.setusername (name);
User.setpassword (password);
if (Getuserservice (). Saveuser (user)) {
return SUCCESS;
}
return ERROR;
}

Public String Update () throws Exception {
Users user = null;
String action = request.getparameter ("action");
String id = request.getparameter ("id");
if ("Loading". Equals (action)) {
user = new Users ();
user = Getuserservice (). Getuserbyid (integer.valueof (id));
if (null! = user) {
Request.setattribute ("user", user);
}
return UPDATE;

} else {
user = new Users ();
User.setusername (name);
User.setpassword (password);
User.setid (integer.valueof (id));
if (Getuserservice (). UpdateUser (user)) {
return SUCCESS;
}
}

return ERROR;
}

Public String Delete () throws Exception {
String id = request.getparameter ("id");
if (Getuserservice (). DeleteUser (integer.valueof (ID))) {
return SUCCESS;
}
return ERROR;
}


Public String Findallusers () throws Exception {
List < Users > list = Getuserservice (). Findallusers ();
if (list.size () > 0) {
Request.setattribute ("list", list);
return SUCCESS;
}
return ERROR;
}

@Override
public void Setservletrequest (HttpServletRequest request) {
this. Request = Request;
}
}
  
Well, now that all of our implementation classes are complete, the rest is the config file.
Eight, began to write the configuration file
1, Configuration Struts.xml
<? XML version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Struts Public
"-//apache software foundation//dtd Struts Configuration 2.0//en"
"Http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< include file = "Struts-default.xml"/>
< package name = "Struts2" extends = "Struts-default" >
< action name = "Logon" class = "Login" >
< result name = "Success" >/success.jsp </result >
< result name = "Input" >/login.jsp </result >
</Action >
< action name = "Save" class = "Login" &

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.