文章标题

简单粗暴
all

1.stu.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="/struts-dojo-tags" prefix="sx"%>
<!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">
<s:head theme="xhtml" />
<sx:head parseContent="true" />
<title>添加学生信息</title>
</head>

<body bgcolor="#E3E3E3">
    <h3>添加学生信息</h3>
    <s:form action="save" method="post" validate="true">
        <table>
            <tr>
                <td>学号:</td>
                <td><s:textfield name="xs.xh" /></td>
            </tr>
            <tr>
                <td>姓名:</td>
                <td><s:textfield name="xs.xm" /></td>
            </tr>
            <tr>
                <td>性别:</td>
                <td><s:radio name="xs.xb" list="#{1:'男', 0:'女'}" /></td>
            </tr>
            <tr>
                <td>专业:</td>
                <td><s:textfield name="xs.zy" /></td>
            </tr>
            <tr>
                <td>出生时间:</td>
                <td><sx:datetimepicker name="xs.cssj" id="cssj"
                        displayFormat="yyyy-MM-dd" /></td>
            </tr>

            <tr>
                <td>备注:</td>
                <td><s:textarea name="xs.bz" height="600" width="300"></s:textarea></td>
            </tr>

            <tr>
                <td><s:submit value="添加" /></td>
                <td><s:reset value="重置" /></td>
            </tr>
        </table>
    </s:form>
</body>

</html>

2.success.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>
    添加成功!
</body>
</html>

3.ccc.hut.model.Student

package ccc.hut.model;

import java.sql.Date;

public class Student {
    private String xh;
    private String xm;
    private byte xb;
    private Date cssj;
    private String zy;

    private String bz;
    public String getXh() {
        return xh;
    }
    public void setXh(String xh) {
        this.xh = xh;
    }
    public String getXm() {
        return xm;
    }
    public void setXm(String xm) {
        this.xm = xm;
    }
    public byte getXb() {
        return xb;
    }
    public void setXb(byte xb) {
        this.xb = xb;
    }
    public Date getCssj() {
        return cssj;
    }
    public void setCssj(Date cssj) {
        this.cssj = cssj;
    }
    public String getZy() {
        return zy;
    }
    public void setZy(String zy) {
        this.zy = zy;
    }

    public String getBz() {
        return bz;
    }
    public void setBz(String bz) {
        this.bz = bz;
    }


}

4.ccc.hut.action.SaveAction

package ccc.hut.action;

import ccc.hut.work.DBConn;
import ccc.hut.model.Student;
import com.opensymphony.xwork2.ActionSupport;

public class SaveAction extends ActionSupport {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Student xs;

    public Student getXs() {
        return xs;
    }

    public void setXs(Student xs) {
        this.xs = xs;
    }

    public String execute() throws Exception {
        DBConn db = new DBConn();
        Student stu = new Student();
        stu.setXh(xs.getXh());
        stu.setXm(xs.getXm());
        stu.setXb(xs.getXb());
        stu.setCssj(xs.getCssj());
        stu.setZy(xs.getZy());
        stu.setBz(xs.getBz());



        if (db.saveStudent(stu)) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }

    public void validate(){
        //如果用户名为空,就把错误信息添加到 Action 类的 fieldErrors
        if(xs.getBz()==null||xs.getBz().trim().equals("")){
            addFieldError("xs.bz","用户名必须填!");

        }       

    }
}

这里添加了必填检验方法,会在执行系统的excute()方法之前执行。如果执行后Action类的fieldErrors中已经包含里数据校验错误信息,将把请求转发到input逻辑视图处,所以在Action配置中加入/login.jsp

5.ccc.hut.work.DBConn

package ccc.hut.work;

import java.sql.*;
import ccc.hut.model.Student;

public class DBConn {
    Connection conn;
    PreparedStatement pstmt;
    public DBConn(){
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;"+"databaseName=struct","sa","123456");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public boolean saveStudent(Student stu) {
        try{
            pstmt=conn.prepareStatement("insert into xsb values(?,?,?,?,?,?)");
            pstmt.setString(1, stu.getXh());
            pstmt.setString(2, stu.getXm());
            pstmt.setByte(3, stu.getXb());
            pstmt.setDate(4, stu.getCssj());
            pstmt.setString(5, stu.getZy());
            pstmt.setString(6, stu.getBz());
            pstmt.executeUpdate();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }

    }
}

6.ccc.hut.tool.MyInterceptor

package ccc.hut.tool;

import ccc.hut.action.SaveAction;
import com.opensymphony.xwork2.*;
import com.opensymphony.xwork2.interceptor.*;

public class MyInterceptor extends AbstractInterceptor{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public String intercept(ActionInvocation arg0) throws Exception{
        // 得到 MainAction 类对象
        SaveAction action=(SaveAction)arg0.getAction();

        if(action.getXs().getXh().equals("asd")||action.getXs().getXh().equals("ASD")){
            return Action.INPUT;
        }
        // 继续执行其他拦截器或 Action 中的方法
        return arg0.invoke();
    }
}

自定义拦截器,对学号为asd或者ASD进行拦截(鸡肋…)

7.struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
    "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="default" extends="struts-default">
        **<interceptors>
            <interceptor name="myInterceptor" class="ccc.hut.tool.MyInterceptor"></interceptor>
        </interceptors>
        <default-interceptor-ref name=""></default-interceptor-ref>**

        <action name="save" class="ccc.hut.action.SaveAction">
            <result name="success">/success.jsp</result>
            <result name="error">/stu.jsp</result>
            <result name="input">/stu.jsp</result>
            <!--拦截器在result后面--!>
            <!--使用系统默认拦截器栈--!>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <!--配置拦截器--!>
            <interceptor-ref name="myInterceptor"></interceptor-ref>
        </action>
    </package>
</struts>  

8.SaveAction-save-validate.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE validators PUBLIC 
          "-//OpenSymphony Group//XWork Validator 1.0//EN" 
          "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">          
<validators>
    <field name ="xs.bz" >
        <!--备注必填--!>
        <field-validator type ="requiredstring">
            <!--去空格--!>
           <param name="trim">true</param>
            <!--错误提示信息--!>
           <message> 不显示这一段而是方法中的提示! </message>
        </field-validator>
    </field >

    </field >


</validators > 

对备注是否为空进行校验(虽然现实很鸡肋…只是试试检验方法…)

9.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>example2</display-name>
  <welcome-file-list>
    <welcome-file>stu.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值