20170925_chr_loginchRegist_DMI 图书管理中的动态方法调用

动态方法调用

  • /20170925_chr_loginchRegist_DMI/src/nuc/sw/action/BookAction.java
package nuc.sw.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import nuc.sw.service.BookService;
import nuc.sw.vo.*;

public class BookAction extends ActionSupport implements ModelDriven<Book> {
    private Book b=new Book();
    private BookService bService=new BookService();
    private List<Book> books=new ArrayList<Book>();
    public List<Book> getBooks() {
        return books;
    }
    @Override
    public Book getModel() {
        // TODO Auto-generated method stub
        return b;
    }
    //添加书籍
    public String addBook() {
        bService.addBook(b);
        return "addOK";
    }
    //查询所有书籍
    public String findAllBook() {
        //引入值栈的概念
        books=bService.findAllBook();
        return "findAllOK";
    }

}
  • /20170925_chr_loginchRegist_DMI/src/nuc/sw/action/LoginAction.java
package nuc.sw.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
  private String username;
  private String password;
  private String info;
  public String getInfo() {
    return info;
  }
  public void setInfo(String info) {
    this.info = info;
  }
  public String getUsername() {
    return username;
  }
  public void setUsername(String username) {
    this.username = username;
  }
  public String getPassword() {
    return password;
  }
  public void setPassword(String password) {
    this.password = password;
  }
    public String loginMethod() throws Exception {
      // TODO Auto-generated method stub
      //接收登陆表单页提交的数据
      if(username.equals("程浩然")&&password.equals("123")) {
          ActionContext.getContext().getSession().put("user",username);
          return SUCCESS;
      }
      else {
          ActionContext.getContext().getSession().put("error","用户名或密码错误");
          return ERROR;
      }
    }
    public void validate() {
        if(username==null||username.trim().equals("")) {
        this.addFieldError("usernameError", "用户名不能为空");
        }
        if(password==null||password.trim().equals("")) {
        this.addFieldError("passwordError", "密码不能为空");
        }
    }
    public String registMethod() throws Exception {
        System.out.println("进入注册逻辑程序!");
        info="请使用注册的用户名和密码登录";
        return "registOK";
    }

}
  • /20170925_chr_loginchRegist_DMI/src/nuc/sw/dao/BookDao.java
package nuc.sw.dao;

import java.util.List;
import nuc.sw.db.BookDB;
import nuc.sw.vo.Book;

public class BookDao {
 public void addBook(Book b) {
     BookDB.books.add(b);
 }
 public List<Book> findAllBook(){
     return BookDB.books;
 }
}
  • /20170925_chr_loginchRegist_DMI/src/nuc/sw/db/BookDB.java
package nuc.sw.db;

import java.util.ArrayList;
import java.util.List;
import nuc.sw.vo.*;

public class BookDB {
 public static List<Book> books=new ArrayList<Book>();
}
  • /20170925_chr_loginchRegist_DMI/src/nuc/sw/service/BookService.java
package nuc.sw.service;

import java.util.List;
import nuc.sw.dao.BookDao;
import nuc.sw.vo.Book;

public class BookService {
 private BookDao bDao=new BookDao();
 public void addBook(Book b) {
    bDao.addBook(b);
 }
 public List<Book> findAllBook(){
    return bDao.findAllBook(); 
 }
}
  • /20170925_chr_loginchRegist_DMI/src/nuc/sw/vo/Book.java
package nuc.sw.vo;

public class Book {
 private String b_name;
 private String b_author;
 private String b_price;
 public String getB_name() {
    return b_name;
 }
 public void setB_name(String b_name) {
    this.b_name = b_name;
 }
 public String getB_author() {
    return b_author;
 }
 public void setB_author(String b_author) {
    this.b_author = b_author;
 }
 public String getB_price() {
    return b_price;
 }
 public void setB_price(String b_price) {
    this.b_price = b_price;
 }

}
  • /20170925_chr_loginchRegist_DMI/src/struts.xml
<struts>
    <!-- Add packages here -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
    <package name="user" namespace="/" extends="struts-default">
      <action name="loginAction" class="nuc.sw.action.LoginAction" method="loginMethod">
       <result name="success">
        /welcome.jsp
       </result>
       <result name="error">
        /login.jsp
       </result>
       <result name="input">
        /login.jsp
       </result>
      </action>
      <action name="registAction" class="nuc.sw.action.LoginAction" method="registMethod">
       <result name="registOK">
        /login.jsp
       </result>
       <result name="input">
        /login.jsp
       </result>
      </action>
      <action name="*Action" class="nuc.sw.action.BookAction" method="{1}">
       <result name="addOK" type="chain">
        findAllBookAction
       </result>
       <result name="findAllOK">
        /showBook.jsp
       </result>
      </action>
     </package>
</struts>
  • /20170925_chr_loginchRegist_DMI/WebContent/addBook.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="addBookAction" method="post">
  <p>书名:<input type="text" name="b_name"></p>
  <p>作者:<input type="text" name="b_author"></p>
  <p>定价:<input type="text" name="b_price"></p>
  <p><input type="submit" value="提交"></p>
 </form>
</body>
</html>
  • /20170925_chr_loginchRegist_DMI/WebContent/login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页</title>
<script type="text/javascript">
 function login(){
     targetForm=document.forms[0];
     targetForm.action="loginAction!loginMethod";
     targetForm.submit();
 }
 function regist(){
     targetForm=document.forms[0];
     targetForm.action="registAction!registMethod";
     targetForm.submit();
 }
</script>
</head>
<body>
  <font color="red">${requestScope.error}</font>
  <font color="red"><s:fielderror></s:fielderror></font>
  <s:property value="info"/>
  <form action="action!methodName" method="post">
    用户名:<input type="text" name="username"><br>
    密&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" name="password"><br>
  <input type="button" value="登陆" onclick="login()">
  <input type="button" value="注册" onclick="regist()">
  </form>
</body>
</html>
  • /20170925_chr_loginchRegist_DMI/WebContent/showBook.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>展示书籍</title>
</head>
<body>
 <table border="1">
  <tr logcolor="red">
   <td>书名</td>
   <td>作者</td>
   <td>定价</td>
  </tr>
  <s:iterator value="books" var="b">
   <tr>
   <td><s:property value="#b.b_name"/></td>
   <td><s:property value="#b.b_author"/></td>
   <td><s:property value="#b.b_price"/></td>
   </tr>
  </s:iterator>
 </table>
</body>
</html>
  • /20170925_chr_loginchRegist_DMI/WebContent/welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎页</title>
</head>
<body>
 欢迎 ${session.type}${session.user} 登陆!<br>
  <hr>
  <a href="addBook.jsp" target="main">增加书籍</a>
  <a href="delateAction">删除书籍</a>
  <a href="modifyAction">修改书籍</a>
  <a href="queryAction">查询书籍</a><br>
  <iframe name="main" widrh="200" height="380">
  </iframe>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值