动态方法调用
/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() {
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 {
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 >
<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 >
密 码:<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 >