ssh框架整合实例子-web层

1.  UserAction类,从DispatchAction 继承过来

 

package com.yanek.demo.ssh.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.yanek.demo.ssh.bean.UserForm;
import com.yanek.demo.ssh.service.UserService;
import com.yanek.demo.ssh.vo.User;

public class UserAction extends DispatchAction {

 private UserService userService;

 public UserService getUserService() {
  return userService;
 }

 public void setUserService(UserService userService) {
  this.userService = userService;
 }

 /*
  * @Override
  *
  *
  * public ActionForward execute(ActionMapping mapping, ActionForm form,
  * HttpServletRequest request, HttpServletResponse response) throws
  * Exception {
  *
  *
  * System.out.println("execuet"); UserForm uf = (UserForm) form; String
  * username = uf.getUsername(); String password = uf.getPassword(); String
  * email = uf.getEmail(); int id = uf.getId();
  *
  * User user = new User(id, username, password, email);
  *
  * userService.save(user);
  *
  * request.setAttribute("user", user);
  *
  * return mapping.findForward("success"); }
  */
 public ActionForward save(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {

  System.out.println("execuet save");

  UserForm uf = (UserForm) form;
  String username = uf.getUsername();
  String password = uf.getPassword();
  String email = uf.getEmail();
  int id = uf.getId();

  User user = new User(id, username, password, email);

  userService.save(user);

  request.setAttribute("user", user);

  return mapping.findForward("success");
 }

 public ActionForward list(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {

  System.out.println("execuet list");

  List users = userService.findAll();

  User t_user;
  for (int i = 0; i < users.size(); i++) {
   t_user = (User) users.get(i);
   System.out.println("username=" + t_user.getUsername() + "--id"
     + t_user.getId());

  }

  request.setAttribute("users", users);

  return mapping.findForward("list");
 }

}

 

2.  UserForm actionForm类

 

 

package com.yanek.demo.ssh.bean;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class UserForm extends ActionForm {

 @Override
 public void reset(ActionMapping arg0, HttpServletRequest arg1) {

  this.username = null;
  this.password = null;
  this.id = 0;
  this.email = null;
 }

 @Override
 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {

  ActionErrors errors = new ActionErrors();

  if (username == null || username.equals("")) {
   errors.add("username", new ActionMessage(
     "hello.error.username.null"));
  }

  if (password == null || password.equals("")) {
   errors.add("password", new ActionMessage(
     "hello.error.password.null"));
  }

  if (email == null || email.equals("")) {
   errors.add("email", new ActionMessage("hello.error.email.null"));
  }

  return errors;

 }

 private int id;

 private String username;

 private String password;

 private String email;

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getPassword() {
  return password;
 }

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

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

}

 

上一篇:SQL Server 2012 AlwaysOn高可用配置之六:启用AlwaysOn功能


下一篇:使用libcurl实现的下载器