利用Struts2技术,action类收不到表单提交的数据
package com.model;
import com.tool.MyDataBase;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Student {
private String id;
private String name;
private int stu_class;
private int grade;
public int getStu_class() {
return stu_class;
}
public void setStu_class(int stu_class) {
this.stu_class = stu_class;
}
private HomeWork homeWork;
public HomeWork getHomeWork() {
return homeWork;
}
public void setHomeWork(HomeWork homeWork) {
this.homeWork = homeWork;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getGrade() {
return grade;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setGrade(int grade) {
this.grade = grade;
}
public Student() {
}
public Student(String id, String name, int stuClass, int grade) {
this.id = id;
this.name = name;
this.stu_class = stuClass;
this.grade = grade;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", stuClass=" + stu_class +
", grade=" + grade +
", homeWork=" + homeWork +
'}';
}
}
action类
public class addStuAction {
private Student stu;
public addStuAction() {
System.out.println("Student初始化完成!!");
}
public String addstu() throws SQLException {
if(stu!=null) {
System.out.println(stu.toString());
Connection con = MyDataBase.getCon();
String sql = "insert into student(id,name,grade,stu_class) value(?,?,?,?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, stu.getId());
ps.setString(2, stu.getName());
ps.setInt(3, stu.getGrade());
ps.setInt(4, stu.getStu_class());
boolean temp = ps.execute();
MyDataBase.close(con, ps, null);
if (temp) {
System.out.println("添加成功!");
return "True";
}
}
return "False";
}
}
struts2配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="AddStu" extends="struts-default" namespace="/">
<action name="addStu" class="com.action.addStuAction" method="addstu">
<result name="True">/Teacher/addStuPage.jsp</result>
<result name="False">/index.jsp</result>
</action>
</package>
</struts>
表单
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>新增学生</title>
<link rel="stylesheet" type="text/css"
href="../css/addStudent.css" >
</head>
<body>
<s:form namespace="/" action="addStu" method="POST">
<div id="all">
<h1>添加学生</h1>
<div id="stuId">
学号 <input type="text" name="stu.id">
</div>
<div id="name" >
姓名 <input type="text" name="stu.name">
</div>
<div id="grade" >
班级 <input type="text" name="stu.grade">
</div>
<div id="stuClass">
年级 <input type="text" name="stu.stu_class">
</div>
<div id="submit">
<button type="submit" id="btns">添加</button>
</div>
<div id="reset">
<button type="reset" id="btn_cl">重置</button>
</div>
</s:form>
</div>
</body>
</html>
结构图