当前位置: 代码迷 >> JavaScript >> Struts2怎么获取JSP页面数据,action 这样写有关问题出在哪里,请大家指教,能否把代码修改一上,多谢
  详细解决方案

Struts2怎么获取JSP页面数据,action 这样写有关问题出在哪里,请大家指教,能否把代码修改一上,多谢

热度:144   发布时间:2012-10-28 09:54:44.0
Struts2如何获取JSP页面数据,action 这样写问题出在哪里,请大家指教,能否把代码修改一下,谢谢
增加学生信息
public String addStudent()throws Exception{
System.out.println("增加学生开始*******************"+studentService);
try{
Student stu = new Student();
stu.setName(name);
stu.setSex(sex);
stu.setAge(age);
stu.setBirthday(birthday);
stu.setEmail(email);
stu.setHomeAddress(homeAddress);
studentService.addStudent(stu);
// HttpServletRequest request = ServletActionContext.getRequest();
// name = request.getParameter("name");
// sex = request.getParameter("sex");
// age = Integer.parseInt(request.getParameter("age"));
// DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
// birthday = format1.parse(request.getParameter("birthday"));
// email = request.getParameter("email");
// homeAddress = request.getParameter("homeAddress");
// Student s = new Student();
// s.setName("wangerxiao");
// s.setSex("W");
// s.setEmail("wangerxiao@163.com");
// student.setAge(20);
// student.setBirthday(new Date());
// s.setHomeAddress("响水县滨海路商业街");
// studentService.addStudent(s);
}catch(Exception ex){
ex.printStackTrace();
}
return SUCCESS;
}

JSP页面代码:
<s:form action="addStudent.action" id="addStudentForm" theme="simple"
name="addStudentForm" onsubmit="checkFroms();return false;">
<s:token/>
<table width="300"  border="0" align="left" cellspacing="1" class="table_1px_form tds">
<tbody>
<tr>
  <td align="center" colspan="2">
<span class="title">学生信息</span>
  </td>
</tr>
<tr>
          <td width="100" align="right">学生姓名 :</td>
   <td width="140" align="left">
<s:textfield name="student.name" id="studentName" theme="simple"/>
   </td>
</tr>
<tr>
           <td width="100" align="right">性别 :</td>
    <td width="140" align="left">
<s:textfield  name="student.sex" id="studentSex"></s:textfield>
    </td>
       </tr>
<tr>
    <td align="right">年龄 :</td>
    <td align="left">
<s:textfield  name="student.age" id="studentAge"></s:textfield>
    </td>
</tr>
<tr>
    <td align="right">出生日期:</td>
    <td align="left">
<s:textfield  name="student.birthday" id="studentBirthday"></s:textfield>
    </td>
</tr>

<tr>
     <td align="right">Email:</td>
     <td align="left">
<s:textfield  name="student.email" id="studentEmail"></s:textfield>
     </td>
</tr>
<tr>
     <td align="right">家庭地址:</td>
     <td align="left">
<s:textfield  name="student.homeAddress" id="studentHomeAddress"></s:textfield>
     </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" id="button" name="button" value="新u22686 "/>
       </td>
</tr>
</tbody>
</table>
</s:form>
    
1 楼 googlename 2009-11-20  
把你的struts.xml,和整个Action类贴出来吧,不然人家怎么帮你改呢。。。。。。一看就是个新手。。。。。
2 楼 ccw 2009-11-20  
好的,谢谢
struts.xml
<action name="addStudent*"
class="org.langwei.struts2.action.housebase.StudentChangeAction" method="addStudent">
    <result  name="success">/housebase/addStudentWeb.jsp</result>
</action>

StudentChangeAction 类
package org.langwei.struts2.action.housebase;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.langwei.struts2.action.BaseAction;
import org.langwei.struts2.persistence.entity.Student;
import org.langwei.struts2.persistence.service.housebase.StudentService;

@SuppressWarnings({ "serial", "unchecked" })
public class StudentChangeAction extends BaseAction{

private int studentId;
private String name;
private int age;
private Date birthday;
private String sex;
private String email;
private String homeAddress;
private StudentService studentService;
private List<Student> students = null;
private Student student;
private String message;
private int id;
private HttpServletRequest request = null;
public StudentChangeAction studentAction;
private List<Student> weblist;

// 显示多少条记录
private int indexNumber;

private Integer state;

private Integer nodeId;

/* 显示Student */

public String StudentDisplay(){
students = studentService.findAllStudent();
return SUCCESS;
}

public String getStudentList() {
System.out.println("***********************");
students = studentService.findAllStudent();

if(students != null){
for(int i=0;i<students.size();i++){
Student student = students.get(i);
System.out.println(student.getStudentId()+"\t");
System.out.println(student.getName()+"\t");
System.out.println(student.getSex()+"\t");
System.out.println(student.getAge()+"\t");
System.out.println(student.getBirthday()+"\t");
System.out.println(student.getEmail()+"\t");
System.out.println(student.getHomeAddress()+"\t");
}
}else{
System.out.println("list null action");
System.out.println(students.size());
}

// List<Student> list=new ArrayList<Student>();
// if(students.size()>= (indexNumber + 1)){
// state=1;
// for(int i=0;i<indexNumber;i++){
// list.add(students.get(i));
// }
// }
// else{
// for(int i=0;i<students.size();i++){
// list.add(students.get(i));
// }
// }
// students=list;
// nodeId=studentService.findactionId("test.action");
return SUCCESS;

}


public String edit(){
request = ServletActionContext.getRequest();
String id = request.getParameter("id");
int ID = Integer.valueOf(id);
studentAction = studentService.edit(ID);
return SUCCESS;
}


public String updateStudent(){


// request = ServletActionContext.getRequest();
// ActionContext ctx = ActionContext.getContext();
// Map m = ctx.getParameters();
// String ms = (String)m.get("saveStudent_sa_id");
// int ID = 0;
// Student student = new Student();
// request = ServletActionContext.getRequest();
// String id = request.getParameter("id");
// student.setStudentId(studentAction.getStudentId());
// student.setName(studentAction.getName());
// student.setSex(studentAction.getSex());
// student.setAge(studentAction.getAge());
// student.setBirthday(studentAction.getBirthday());
// student.setEmail(studentAction.getEmail());
// student.setHomeAddress(studentAction.getHomeAddress());
// studentService.updateStudent(student);

// String id = request.getParameter("id");
// student = studentService.findStudentById(Integer.valueOf(id));
// if(null != student ){
// request.setAttribute("student", student);
// }
// else{
// student = new Student();
// student.setStudentId(Integer.valueOf(id));
// student.setName(name);
// student.setSex(sex);
// student.setAge(age);
// student.setBirthday(birthday);
// student.setEmail(email);
// student.setHomeAddress(homeAddress);
// studentService.addStudent(student);
// this.setActionName();
// return SUCCESS;
// }

// studentService.updateStudent(student);
// this.setActionName();
return SUCCESS;
}

public String updateStudentWeb(){

student = studentService.findStudentById(id);
return SUCCESS;
}

public String deleteStudent(){
String id= request.getParameter("id");
System.out.println("ID="+ id);
studentService.delStudent(Integer.valueOf(id));
this.setActionName();
return SUCCESS;
}

// public Student getAllStudent(){
// System.out.println("增加学生开始*******************"+studentService);

// ServletRequest request=ServletActionContext.getRequest();
// String name = request.getParameter("name");
// String sex = request.getParameter("sex");
// String age = request.getParameter("age");
// String birthday = request.getParameter("birthday");
// String email = request.getParameter("email");
// String homeAddress = request.getParameter("homeAddress");
// Student s = new Student();
// s.setName(name);
// s.setSex(sex);
// s.setEmail(email);
// student.setAge(Integer.parseInt(age));
// student.setBirthday(new Date(birthday));
// s.setHomeAddress(homeAddress);
// return s;
//   }

public String addStudentWeb(){

return returnStr;
}

@SuppressWarnings("deprecation")
public String addStudent()throws Exception{
System.out.println("增加学生开始*******************"+studentService);
try{
HttpServletRequest request = ServletActionContext.getRequest();
Student stu = new Student();
System.out.println("name="+request.getParameter(name));
stu.setName(name);
stu.setSex(sex);
stu.setAge(age);
stu.setBirthday(birthday);
stu.setEmail(email);
stu.setHomeAddress(homeAddress);
studentService.addStudent(stu);


// HttpServletRequest request = ServletActionContext.getRequest();
// name = request.getParameter("name");
// sex = request.getParameter("sex");
// age = Integer.parseInt(request.getParameter("age"));
// DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
// birthday = format1.parse(request.getParameter("birthday"));
// email = request.getParameter("email");
// homeAddress = request.getParameter("homeAddress");
// Student s = new Student();
// s.setName("wangerxiao");
// s.setSex("W");
// s.setEmail("wangerxiao@163.com");
// student.setAge(20);
// student.setBirthday(new Date());
// s.setHomeAddress("响水县滨海路商业街");
// studentService.addStudent(s);
}catch(Exception ex){
ex.printStackTrace();
}
return SUCCESS;
}


@Override
public String execute() throws Exception {
// TODO 自动生成方法存根
return "SUCCESS";
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getHomeAddress() {
return homeAddress;
}

public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public int getStudentId() {
return studentId;
}

public void setStudentId(int studentId) {
this.studentId = studentId;
}

public StudentService getStudentService() {
return studentService;
}

public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public List<Student> getStudents() {
return students;
}

public void setStudents(List<Student> students) {
this.students = students;
}

public StudentChangeAction(){

}

public int getIndexNumber() {
return indexNumber;
}

public void setIndexNumber(int indexNumber) {
this.indexNumber = indexNumber;
}

public Integer getNodeId() {
return nodeId;
}

public void setNodeId(Integer nodeId) {
this.nodeId = nodeId;
}

public Integer getState() {
return state;
}

public void setState(Integer state) {
this.state = state;
}

public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}

@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}

public List<Student> getWeblist() {
return weblist;
}

public void setWeblist(List<Student> weblist) {
this.weblist = weblist;
}
}


  相关解决方案