S2H action+struts.xml+jsp

StudentAction 控制器

package cn.action;
import java.util.ArrayList;
import java.util.List;
import cn.biz.StudentBiz;
import cn.biz.impl.StudentBizImpl;
import cn.entity.Classes;
import cn.entity.Student;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 学生 控制器
 * */
@SuppressWarnings("serial")
public class StudentAction extends ActionSupport {
	private StudentBiz studentBiz = new StudentBizImpl();
	private List<Student> students = new ArrayList<Student>();
	private Student student = new Student();
	private Integer id;
	private List<Classes> classesList = new ArrayList<Classes>();
	private Integer classId;
	/**
	 * 显示学生列表
	 * @return
	 */
	public String list(){
		students = studentBiz.getAll();
		return SUCCESS;
	}
	/**
	 * 根据 id 查询学生信息
	 * @return
	 */
	public String selected(){
		student = studentBiz.getStudentById(id);
		classesList = studentBiz.getClassesList();
		return SUCCESS;
	}
	/**
	 * 修改学生信息
	 * @return
	 */
	public String updated(){
		Classes classes = new Classes();
		classes = studentBiz.getClassesById(classId);
		student.setClasses(classes);
		if(studentBiz.update(student) > 0){
			students = studentBiz.getAll();
			return SUCCESS;
		}else{
			return INPUT;
		}
		
	}
	public StudentBiz getStudentBiz() {
		return studentBiz;
	}
	public void setStudentBiz(StudentBiz studentBiz) {
		this.studentBiz = studentBiz;
	}
	public List<Student> getStudents() {
		return students;
	}
	public void setStudents(List<Student> students) {
		this.students = students;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public List<Classes> getClassesList() {
		return classesList;
	}
	public void setClassesList(List<Classes> classesList) {
		this.classesList = classesList;
	}
	public Integer getClassId() {
		return classId;
	}
	public void setClassId(Integer classId) {
		this.classId = classId;
	}
}

 

struts.xml 配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd">
<struts>
	<constant name="struts.ui.theme" value="simple"></constant>
	<package name="default" namespace="/" extends="struts-default">
		<!-- 显示学生列表 -->
		<action name="list" class="cn.action.StudentAction" method="list">
			<result name="success">/index.jsp</result>
		</action>
		<!-- 根据id查询学生信息 -->
		<action name="selected" class="cn.action.StudentAction" method="selected">
			<result name="success">/update.jsp</result>
		</action>
		<!-- 修改学生信息 -->
		<action name="updated" class="cn.action.StudentAction" method="updated">
			<result name="success">/index.jsp</result>
			<result name="input">/update.jsp</result>
		</action>
	</package>
</struts>    

 

index.jsp 页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
*{ margin:0; padding:0; }
body{ font-family:"宋体"; color:#333; font-size:14px; background:#fff;}
a{ color:#069; text-decoration:underline;}
a:hover{ color:#f60; text-decoration:underline;}
h3{ text-align:center; line-height:40px;}
table{ margin:0 auto; line-height:23px;}
table tr th{ background:#B8860B;}
table tr td{ padding:0 8px;}
table .tdBg td{ background:#999}
</style>
<title>学生信息列表</title>
</head>
<body>
<h3>学生信息列表</h3>
<table border="1">
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>生日</th>
        <th>电话</th>
        <th>E-mail</th>
        <th>班级</th>
    </tr>
    <s:iterator value="students" status="st"> 
    <tr <s:if test="#st.even">class="tdBg"</s:if>  ><!-- 隔行变色 -->
        <td><s:a href="selected.action?id=%{id}"><s:property value="id"/></s:a></td>
        <td><s:property value="sname"/></td>
        <td><s:property value="gender"/></td>
        <td><s:date name="birthday" format="yyyy-MM-dd" /></td>
        <td><s:property value="telephone"/></td>
        <td><s:property value="email"/></td>
        <td><s:property value="classes.cname"/></td>
    </tr>
    </s:iterator>
</table>
</body>
</html>

 

效果图:
S2H action+struts.xml+jsp
 

update.jsp 页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
*{ margin:0; padding:0; }
body{ font-family:"宋体"; color:#333; font-size:14px; background:#fff;}
h3{ text-align:center; line-height:40px;}
table{ margin:0 auto; line-height:23px;}
table input[type="text"]{ border:1px solid #333; height:18px; line-height:18px; padding-left:6px;}
table select{ border:1px solid #333; }
</style>
<script type="text/javascript">
//根据 id 获取元素  
function $(id) {  
	return document.getElementById(id);  
}
//开机加载  
window.onload = function() {  
	//为表单注入 onsubmit 事件  
	document.updatedForm.onsubmit = function() {  
	return check();  
	};  
};  
//提交表单前验证  
function check() {  
	var sname = $("sname");  
	var birthday = $("birthday"); 
	var telephone = $("telephone");
	var email = $("email");
	 
	if ("" == sname.value || "" == birthday.value|| "" == telephone.value|| "" == email.value) {  
    	alert("姓名,生日,电话和E-mail不能为空");  
		return false;  
	}  
	var regEmail = /^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/;  
    if(regEmail.test(email.value)==false){
    	alert("电子邮箱格式不正确,请重新输入");  
        return false;  
    }
	return true;  
}  
</script>
<title>修改学员信息</title>
</head>
<body>
<h3>修改学员信息</h3>
<s:form action="updated" method="post" name="updatedForm">
<table>
    <tr>
        <td><input type="hidden" name="student.id" value="<s:property value='student.id'/>"/>姓名</td>
        <td><input type="text" name="student.sname" id="sname" value="<s:property value='student.sname'/>" /></td>
    </tr>
     <tr>
        <td>性别</td>
        <td>
        	 <s:if test='student.gender =="男"'>
				<input type="radio" value="男" name="student.gender" checked="checked" />男
				<input type="radio" value="女" name="student.gender" />女
			</s:if>
			<s:else>
				<input type="radio" value="男" name="student.gender" />男
				<input type="radio" value="女" name="student.gender" checked="checked"  />女
			</s:else>
        </td>
    </tr>
     <tr>
        <td>生日</td>
        <td><input type="text" name="student.birthday" id="birthday" value="<s:date name='student.birthday' format='yyyy-MM-dd' />" /></td>
    </tr>
     <tr>
        <td>电话</td>
        <td><input type="text" name="student.telephone" id="telephone" value="<s:property value='student.telephone'/>" /></td>
    </tr>
     <tr>
        <td>E-mail</td>
        <td><input type="text" name="student.email" id="email" value="<s:property value='student.email'/>" /></td>
    </tr>
     <tr>
        <td>班级</td>
        <td>
        	<select name="classId">
        		<s:iterator value="classesList">
        			<s:if test="student.classes.id == id">
        			<option value="<s:property value='id'/>" selected="selected"><s:property value="cname"/></option>
        			</s:if>
        			<s:else>
        				<option value="<s:property value='id'/>"><s:property value="cname"/></option>
        			</s:else>
        		</s:iterator>
            </select>
        </td>
    </tr>
    <tr><td colspan="2" style="text-align:center"><input type="submit" value="保存" />&nbsp;&nbsp;<input type="reset" value="重置" /></td></tr>
</table>
</s:form>
</body>
</html>

 

效果图:
S2H action+struts.xml+jsp
 

 

 

你可能感兴趣的:(struts.xml)