注册和管理信息(无连数据库)
一、 建立一个网站,名叫:j2ee03
二、 将struts2核心jar包导入
三、 建立一个action包,用来放操作类,包名:com.j2ee03.web.action
四、 配置web.xml和struts.xml文件,完整的代码参照最后。
五、 修改index.jsp,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>我的第三个网站</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <a href="register.jsp">注册</a><br/> <a href="usermanager.jsp">用户管理</a><br/> </body> </html>
六、 新建register.jsp,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>用户注册</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="My97DatePicker/WdatePicker.js"></script> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <s:head theme="ajax" /> </head> <body> <form action="addoneuser.action" method="post" name="form1"> <table border="1"> <tr> <td>用户名(学号):</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="userpwd" /></td> </tr> <tr> <s:textfield name="phone" label="电话"></s:textfield> </tr> <tr> <td>出生日期:</td> <td><input id="birthday" name="birthday" type="text" onClick="WdatePicker()" /></td> </tr> <tr> <td>性别:</td> <td><input id="sex" name="sex" type="radio" value="男" checked="checked" /> 男 <input id="sex" name="sex" type="radio" value="女" /> 女</td> </tr> <tr> <td><input type="submit" value="提交" /></td> <td><input type="reset" value="重置" /></td> </tr> </table> </form> </body> </html>
七、 新建addresult.jsp,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>添加情况</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <font color="red"><s:actionerror/></font> <br> 您的用户名是: <s:property value="username"/><br/> 您的密码是: <s:property value="userpwd"/><br/> 您的出生日期是: <s:property value="birthday"/><br/> 您的电话是: <s:property value="phone"/><br/> 您的性别是: <s:property value="sex"/><br/> <a href="index.jsp">返回首页</a> </body> </html>
八、 新建usermanager.jsp,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>学生管理</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <font color="red"><s:actionerror/></font> <table border="1"> <tr> <th>序号</th> <th>用户名</th> <th>操作</th> </tr> <tr> <td>1</td> <td>1306405021</td> <td> <a href="searchoneuser.action?username=1306405021">查看</a> <a href="editoneuser.action?username=1306405021">编辑</a> <a href="deloneuser.action?username=1306405021">删除</a> </td> </tr> <tr> <td>2</td> <td>1306405022</td> <td> <a href="searchoneuser.action?username=1306405022">查看</a> <a href="editoneuser.action?username=1306405022">编辑</a> <a href="deloneuser.action?username=1306405022">删除</a> </td> </tr> </table> <a href="index.jsp">返回首页</a> </body> </html>
九、 web.xml配置方法如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
十、 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> <package name="p1" extends="struts-default" namespace="/"> <action name="*user" class="com.yzx.web.action.UsermanagerAction" method="{1}"> <result name="addresult">addresult.jsp</result> <result name="usernamager">usermanager.jsp</result> </action> </package> </struts>
十一、 学生操作类UsermanagerAction.java代码如下:
package com.yzx.web.action; import com.opensymphony.xwork2.ActionSupport; public class UsermanagerAction extends ActionSupport{ private String username; private String userpwd; private String birthday; private String phone; private String sex; public String addone(){ username=username.trim(); userpwd=userpwd.trim(); System.out.println("用户名是:"+username); System.out.println("密码是:"+userpwd); System.out.println("出生日期是:"+birthday); System.out.println("电话是:"+phone); System.out.println("性别是:"+sex); if(username==null || username.length()<1) { addActionError("用户名必填"); return "addresult"; } if(userpwd==null || userpwd.length()<6) { addActionError("密码必填,且不小于6位"); return "addresult"; } addActionError("哈哈,你注册成功!!"); return "addresult"; } public String searchone(){ System.out.println("查看的学生学号是:"+username); addActionError("操作的是查看学生的学号:"+username); return "usernamager"; } public String editone(){ System.out.println("编辑的学生学号是:"+username); addActionError("操作的是编辑学生的学号:"+username); return "usernamager"; } public String delone(){ System.out.println("删除的学生学号是:"+username); addActionError("操作的是删除学生的学号:"+username); return "usernamager"; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpwd() { return userpwd; } public void setUserpwd(String userpwd) { this.userpwd = userpwd; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
十二、 工程结构图如下: