首页 > 代码库 > struts2-7-实验一(登录注册校验、管理员增查书籍信息)

struts2-7-实验一(登录注册校验、管理员增查书籍信息)

一:首页login.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 8 <title>登录页面</title> 9 <script>10    function login(){11        targetForm=document.forms[0];12        targetForm.action="Login!LoginMethod";13        targetForm.submit();14    }15    function OrdLogin(){16        17         targetForm=document.forms[0];18         targetForm.action="OrdLogin!LoginMethod";19         targetForm.submit();  20        21    }22     function register(){23         targetForm=document.forms[0];24         targetForm.action="Register!RegisterMethod";25         targetForm.submit();26     }27 </script>28 </head>29 30 <body>31  <s:fielderror escape="false"></s:fielderror>32  <font color="red">${requestScope.error}</font>33  <form action="actionName!methodName" method="post" >34   用户名:<input type="text" name="username"><br>35   密码:<input type="text" name="password"><br>36  用户类型:37         <select name="usertype">38             <option value="管理员" >管理员</option>39             <option value="普通用户">普通用户</option>40         </select>41         <br>42  <input type="button" value="登录"  ${sessionScope.loginType=="管理员"?"onclick=‘login()‘":"onclick=‘OrdLogin()‘"}>43  <input type="button" value="注册"  onclick="register()">44  </form>45 </body>46 </html>

 

 技术分享

 

二:登录注册按钮所对应的事件-->LoginRegAction.java

 1 package nuc.sw.action; 2  3 import java.util.ArrayList; 4 import java.util.List; 5  6 import com.opensymphony.xwork2.ActionContext; 7 import com.opensymphony.xwork2.ActionSupport; 8 import com.opensymphony.xwork2.ModelDriven; 9 10 import nuc.sw.vo.User;11 12 public class LoginRegAction extends ActionSupport implements ModelDriven<User>{13     private User user=new User();14    15 //contextPath server.xml16     17     public User getModel() {18         return user;19     };20 //action    21         public String LoginMethod() throws Exception {22             // TODO Auto-generated method stub23             //登录逻辑24             if(user.getUsername().equals("张丹")&&user.getPassword().equals("123")){25                 ActionContext.getContext().getSession().put("loginUser",user.getUsername());26                 ActionContext.getContext().getSession().put("loginPass",user.getPassword());27                 ActionContext.getContext().getSession().put("loginType",user.getUsertype());28                 return SUCCESS;29             }30             else{31                 32                 ActionContext.getContext().put("error", "用户名或者密码错误");33                 return ERROR;34             }35         }36         public String RegisterMethod() throws Exception {37             // TODO Auto-generated method stub38             if(user.getUsername().equals("张丹")&&user.getPassword().equals("123")){39                 ActionContext.getContext().getSession().put("registerUser",user.getUsername());40                 ActionContext.getContext().getSession().put("registerPass",user.getPassword());41                 ActionContext.getContext().getSession().put("registerType",user.getUsertype());42                 return SUCCESS;43             }44             else{45                 46                 ActionContext.getContext().put("error", "用户名或者密码错误");47                 return ERROR;48             }49         }50         51        52     @Override53     public void validate() {54         // TODO Auto-generated method stub55         if(user.getUsername()==null||user.getUsername().trim().equals(""))56             this.addFieldError("usernameError", "<font color=‘red‘>用户名不能为空</font>");57         if(user.getPassword()==null||user.getPassword().trim().equals(""))58             this.addFieldError("passwordError", "<font color=‘red‘>密码不能为空</font>");59         60     }61 }

 

 

三:管理员登录成功后-->adminWelcome.jsp-->可对书籍增删改查

 1 <%@ page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body>10 欢迎${sessionScope.loginType}${sessionScope.loginUser}<a href="login.jsp">登录!</a><br>11 12 <a href="addBook.jsp">添加书籍信息</a>&nbsp;&nbsp;13 <a href="deleteBookInfo.jsp">删除书籍信息</a>&nbsp;&nbsp;14 <a href="updateBookInfo.jsp">修改书籍信息</a>&nbsp;&nbsp;15 <a href="showBookInfo.jsp">查看书籍信息</a>16 </body>17 </html>

 技术分享

 

技术分享

 

四:管理员增加书籍信息-->addBook.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>添加书籍信息</title> 8 </head> 9 <body>10 <form action="AddBookAction" method="post">11 12 欢迎${sessionScope.loginType}${sessionScope.loginUser}<a href="login.jsp">登录!</a><br>13 <a href="addBook.jsp">添加书籍信息</a>&nbsp;&nbsp;14 <a href="deleteBookInfo.jsp">删除书籍信息</a>&nbsp;&nbsp;15 <a href="updateBookInfo.jsp">修改书籍信息</a>&nbsp;&nbsp;16 <a href="showBookInfo.jsp">查看书籍信息</a>17  <table>18  <tr>19   <td>书名:</td>20   <td><input type="text" name="bookName"></td>21  </tr>22  <tr>23   <td>作者:</td>24   <td><input type="text" name="authorName"></td>25  </tr>26  <tr>27   <td>定价:</td>28   <td><input type="text" name="bookPrice"></td>29  </tr>30  <tr>31   <td><input type="submit" value="添加"></td>32  </tr>33 </table>34 </form>35 </body>36 </html>

 

技术分享

 

五:添加按钮对应的事件-->AddBookAction.java

 1 package nuc.sw.action; 2  3 import java.util.ArrayList; 4 import java.util.List; 5  6 import com.opensymphony.xwork2.ActionContext; 7 import com.opensymphony.xwork2.ActionSupport; 8 import com.opensymphony.xwork2.ModelDriven; 9 10 import nuc.sw.vo.Book;11 12 public class AddBookAction extends ActionSupport implements ModelDriven<Book>{13      private Book book=new Book();14 15     @Override16     public Book getModel() {17         // TODO Auto-generated method stub18         return book;19     }20      public String addBook() throws Exception {21             // TODO Auto-generated method stub22             List<Book> list;23             if(ActionContext.getContext().getSession().get("booklist")==null){24                 list= new ArrayList<Book>();25                 list.add(book);26                 ActionContext.getContext().getSession().put("booklist", list);27                 28             }29             else{30                 list=(ArrayList<Book>)ActionContext.getContext().getSession().get("booklist");31                 list.add(book);32                 ActionContext.getContext().getSession().put("booklist", list);33             }34             35             ActionContext.getContext().getSession().put("booklist", list);36             return SUCCESS;37             38         }39 }

 

 

六:添加书籍成功,显示书籍信息-->showBookInfo.jsp

 1 <%@page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8" import="java.util.*,nuc.sw.vo.Book"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body>10 欢迎${sessionScope.loginType}${sessionScope.loginUser}<a href="login.jsp">登录!</a><br>11 12 <a href="addBook.jsp">添加书籍信息</a>&nbsp;&nbsp;13 <a href="deleteBookInfo.jsp">删除书籍信息</a>&nbsp;&nbsp;14 <a href="updateBookInfo.jsp">修改书籍信息</a>&nbsp;&nbsp;15 <a href="showBookInfo.jsp">查看书籍信息</a>16 <br>17     <table border="1">18          <caption>全部书籍信息</caption>19          <tr>20           <td>书名:</td>21           <td>作者:</td>22          <td>定价:</td>23   </tr>24   <% 25         Iterator<Book> iter=((ArrayList<Book>)session.getAttribute("booklist")).iterator();  26         while(iter.hasNext()){27           pageContext.setAttribute("book",iter.next());28    %>29      30     <tr>31       <td>${book.bookName} </td>32       <td>${book.authorName} </td>33       <td>${book.bookPrice} </td>34    </tr>    35 <%36     }37 %>38 39 </table>40 </body>41 </html>

 

 技术分享

 

技术分享

 

技术分享

 

七:管理员和普通用户注册成功后的页面-->regLogin.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>注册成功后登录</title> 8 </head> 9 <body>10 欢迎${sessionScope.registerType}${sessionScope.registerUser}注册,<a href="login.jsp">请登录!</a>11 </body>12 </html>

 技术分享

技术分享

 

 

技术分享

 

技术分享

 

 

八:普通用户登录成功后-->ordWelcome.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>普通用户登录</title> 8 </head> 9 <body>10 欢迎${sessionScope.loginType}${sessionScope.loginUser}<a href="login.jsp">登录!</a>11 </body>12 </html>

 

 技术分享

 

技术分享

 

九:用户和书本建立模型-->nuc.sw.vo-->User.java --   Book.java

 1 package nuc.sw.vo; 2  3 public class User { 4     private String username; 5     private String password; 6     private String usertype; 7     public String getUsername() { 8         return username; 9     }10     public void setUsername(String username) {11         this.username = username;12     }13     public String getPassword() {14         return password;15     }16     public void setPassword(String password) {17         this.password = password;18     }19     public String getUsertype() {20         return usertype;21     }22     public void setUsertype(String usertype) {23         this.usertype = usertype;24     }25     26 }

 

 

 1 package nuc.sw.vo; 2  3 public class Book { 4       private String bookName; 5       private String authorName; 6       private float bookPrice; 7     public String getBookName() { 8         return bookName; 9     }10     public void setBookName(String bookName) {11         this.bookName = bookName;12     }13     public String getAuthorName() {14         return authorName;15     }16     public void setAuthorName(String authorName) {17         this.authorName = authorName;18     }19     public Float getBookPrice() {20         return bookPrice;21     }22     public void setBookPrice(Float bookPrice) {23         this.bookPrice = bookPrice;24     }25 }

 

十:struts.xml

 1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4     "http://struts.apache.org/dtds/struts-2.3.dtd"> 5  6 <struts> 7  <constant name="struts.devMode" value="true" /> 8  <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> 9  <package name="default" namespace="/" extends="struts-default">10     <action name="Login"  method="LoginMethod" class="nuc.sw.action.LoginRegAction">11          <result name="success">/adminWelcome.jsp</result>12          <result name="error">/login.jsp</result>13          <result name="input">/login.jsp</result>14      </action>15      <action name="OrdLogin"  method="OrdLoginMethod" class="nuc.sw.action.LoginRegAction">16          <result name="success">/ordWelcome.jsp</result>17          <result name="error">/login.jsp</result>18          <result name="input">/login.jsp</result>19      </action>20     <action name="Register"  method="RegisterMethod" class="nuc.sw.action.LoginRegAction">21          <result name="success">/regLogin.jsp</result>22          <result name="error">/login.jsp</result>23          <result name="input">/login.jsp</result>24      </action>25      <action name="AddBookAction"  method="addBook" class="nuc.sw.action.AddBookAction">26          <result name="success">/showBookInfo.jsp</result>27          <result name="error">/addBook.jsp</result>28      </action>29  </package>30     <!-- Add packages here -->31 </struts>

 

 

十一:项目结构

技术分享

 

struts2-7-实验一(登录注册校验、管理员增查书籍信息)