jsp struts hibernate2问题,在jsp页面中如何将一个表单里的一个表格中的参数传递给action,action又怎么接收!

Struts2如何获取JSP页面数据,action 这样写问题出在哪里,请大家指教,能否把代码修改一下,谢谢 - ITeye问答
增加学生信息
public String addStudent()throws Exception{
System.out.println("增加学生开始*******************"+studentService);
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("");
//
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();"&
&s:token/&
&table width="300"& border="0" align="left" cellspacing="1" class="table_1px_form tds"&
& &td align="center" colspan="2"&
&span class="title"&学生信息&/span&
&tr&
&&&&&&&&& &td width="100" align="right"&学生姓名 :&/td&
&& &td width="140" align="left"&
&s:textfield name="student.name" id="studentName" theme="simple"/&
&tr&
&&&&&&&&&& &td width="100" align="right"&性别 :&/td&
&&& &td width="140" align="left"&
&s:textfield& name="student.sex" id="studentSex"&&/s:textfield&
&&& &/td&
&&&&&& &/tr&
&&& &td align="right"&年龄 :&/td&
&&& &td align="left"&
&s:textfield& name="student.age" id="studentAge"&&/s:textfield&
&&& &td align="right"&出生日期:&/td&
&&& &td align="left"&
&s:textfield& name="student.birthday" id="studentBirthday"&&/s:textfield&
&&&& &td align="right"&Email:&/td&
&&&& &td align="left"&
&s:textfield& name="student.email" id="studentEmail"&&/s:textfield&
&&&& &/td&
&&&& &td align="right"&家庭地址:&/td&
&&&& &td align="left"&
&s:textfield& name="student.homeAddress" id="studentHomeAddress"&&/s:textfield&
&&&& &/td&
&td colspan="2" align="center"&
&input type="submit" id="button" name="button" value="新u22686 "/&
&&&&&& &/td&
&/table&
&/s:form&
问题补充好的,谢谢
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.
import java.text.DateF
import java.text.SimpleDateF
import java.util.D
import java.util.L
import javax.servlet.ServletR
import javax.servlet.http.HttpServletR
import org.apache.struts2.ServletActionC
import org.langwei.struts2.action.BaseA
import org.langwei.struts2.persistence.entity.S
import org.langwei.struts2.persistence.service.housebase.StudentS
@SuppressWarnings({ "serial", "unchecked" })
public class StudentChangeAction extends BaseAction{
private int studentId;
private String homeA
private StudentService studentS
private List&Student& students =
private HttpServletRequest request =
public StudentChangeAction studentA
private List&Student&
// 显示多少条记录
private int indexN
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");
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&indexNi++){
//
list.add(students.get(i));
//
for(int i=0;i&students.size();i++){
//
list.add(students.get(i));
//
students=
//
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);
//
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);
//
public String addStudentWeb(){
return returnS
@SuppressWarnings("deprecation")
public String addStudent()throws Exception{
System.out.println("增加学生开始*******************"+studentService);
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("");
//
student.setAge(20);
//
student.setBirthday(new Date());
//
s.setHomeAddress("响水县滨海路商业街");
//
studentService.addStudent(s);
}catch(Exception ex){
ex.printStackTrace();
return SUCCESS;
public String execute() throws Exception {
// TODO 自动生成方法存根
return "SUCCESS";
public int getAge() {
public void setAge(int age) {
this.age =
public Date getBirthday() {
public void setBirthday(Date birthday) {
this.birthday =
public String getEmail() {
public void setEmail(String email) {
this.email =
public String getHomeAddress() {
return homeA
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeA
public String getName() {
public void setName(String name) {
this.name =
public String getSex() {
public void setSex(String sex) {
this.sex =
public int getStudentId() {
return studentId;
public void setStudentId(int studentId) {
this.studentId = studentId;
public StudentService getStudentService() {
return studentS
public void setStudentService(StudentService studentService) {
this.studentService = studentS
public String getMessage() {
public void setMessage(String message) {
this.message =
public List&Student& getStudents() {
public void setStudents(List&Student& students) {
this.students =
public StudentChangeAction(){
public int getIndexNumber() {
return indexN
public void setIndexNumber(int indexNumber) {
this.indexNumber = indexN
public Integer getNodeId() {
return nodeId;
public void setNodeId(Integer nodeId) {
this.nodeId = nodeId;
public Integer getState() {
public void setState(Integer state) {
this.state =
public Student getStudent() {
public void setStudent(Student student) {
this.student =
public void setServletRequest(HttpServletRequest request) {
this.request =
public List&Student& getWeblist() {
public void setWeblist(List&Student& weblist) {
this.weblist =
页面部分这样吧
JSP页面代码:
&s:form action="addStudent.action" id="addStudentForm" theme="simple"
name="addStudentForm" onsubmit="checkFroms();"&
&s:token/&
&table width="300"
border="0" align="left" cellspacing="1" class="table_1px_form tds"&
&td align="center" colspan="2"&
&span class="title"&学生信息&/span&
&td width="100" align="right"&学生姓名 :&/td&
&td width="140" align="left"&
&s:textfield name="name" id="studentName" theme="simple"/&
&td width="100" align="right"&性别 :&/td&
&td width="140" align="left"&
&s:textfield
name="sex" id="studentSex"&&/s:textfield&
&td align="right"&年龄 :&/td&
&td align="left"&
&s:textfield
name="age" id="studentAge"&&/s:textfield&
&td align="right"&出生日期:&/td&
&td align="left"&
&s:textfield
name="birthday" id="studentBirthday"&&/s:textfield&
&td align="right"&Email:&/td&
&td align="left"&
&s:textfield
name="email" id="studentEmail"&&/s:textfield&
&td align="right"&家庭地址:&/td&
&td align="left"&
&s:textfield
name="homeAddress" id="studentHomeAddress"&&/s:textfield&
&td colspan="2" align="center"&
&input type="submit" id="button" name="button" value="新u22686 "/&
表单上的name属性必需和你的bean属性一致,还有你set/get了没有,如果你set/get,算我没有说,
&s:textfield& name="student.email" id="studentEmail"&&/s:textfield&
其中 name="student.eamil"打开页面 查看源代码,看一看name里边显示是不是和你的bean里对应的属性名字一致,如果检查无误,好好找一找配置文件看一看那里出错误啦
我有一次就因为一个空格郁闷了二天
&&& Strut 2的Action类通过属性可以获得所有相关的值,如请求参数、Action配置参数、向其他Action传递属性值(通过chain结果)等等。要获得这些参数值,我们要做的唯一一件事就是在Action类中声明与参数同名的属性,在Struts 2调用Action类的Action方法(默认是execute方法)之前,就会为相应的Action属性赋值。
  要完成这个功能,有很大程度上,Struts 2要依赖于ValueStack对象。这个对象贯穿整个Action的生命周期(每个Action类的对象实例会拥有一个ValueStack对象)。当 Struts 2接收到一个.action的请求后,会先建立Action类的对象实例,但并不会调用Action方法,而是先将Action类的相应属性放到 ValueStack对象的顶层节点(ValueStack对象相当于一个栈)。只是所有的属性值都是默认的值,如String类型的属性值为 null,int类型的属性值为0等。
  在处理完上述工作后,Struts 2就会调用拦截器链中的拦截器,当调用完所有的拦截器后,最后会调用Action类的Action方法,在调用Action方法之前,会将 ValueStack对象顶层节点中的属性值赋给Action类中相应的属性。大家要注意,在这里就给我们带来了很大的灵活性。也就是说,在Struts 2调用拦截器的过程中,可以改变ValueStack对象中属性的值,当改变某个属性值后,Action类的相应属性值就会变成在拦截器中最后改变该属性的这个值。
  从上面的描述很容易知道,在Struts 2的的Action类可以获得与属性同名的参数值就是通过不同的拦截器来处理的,如获得请求参数的拦截器是params,获得Action的配置参数的拦截器是staticParams等。在这些拦截器内部读取相应的值,并更新ValueStack对象顶层节点的相应属性的值。而ValueStack对象就象一个传送带,将属性值从一个拦截器传到了另一个拦截器(当然,在这其间,属性值可能改变),最后会传到Action对象,并将ValueStack对象中的属性的值终值赋给Action类的相应属性。
建议对Struts2值栈多加深点理解。
action中的属性:name是和页面上的姓名的 name属性是一直的,所有的都是这样,要不然,一定拿不到这两个不一致的数据,要不是空的。
不用的代码最好都删了,太多了。看着不舒服。
把你的struts.xml,和整个Action类贴出来吧,不然人家怎么帮你改呢。。。。。。一看就是个新手。。。。。
已解决问题
未解决问题java,jsp,Struts2,Action简单传值问题。怎么办? 三路知识网
java,jsp,Struts2,Action简单传值问题。怎么办?
来源:网络收集 & 发布时间: &
错误是:说你test1.jsp的第16行里边,s:if这个标签里边不能用表达式(#username),要不你就用el表达式吧 <s:set name="username" value="<%
本文地址:https://www.slzays.com/0802.html
java,jsp,Struts2,Action简单传值问题 ……你是想用struts2传值吧,首先你要先在配置文件定义好action和要返回的视图,然后把请求提交给这个action,这里面要定义一个成员变量,并提供get和set方法,但这个名字必须是id,...……
java,jsp,Struts2,Action简单传值问题。怎么办? ……错误是:说你test1.jsp的第16行里边,s:if这个标签里边不能用表达式(#username),要不你就用el表达式吧 &s:set name="username" value="&%……
java,jsp,Struts2,Action简单传值问题 ……错误是:说你test1.jsp的第16行里面,s:if这个标签里面不能使用表达式(#username),要不你就用el表达式吧 &s:set name="username" value="&%……
struts2中java文件与jsp如何传值
……一般是在Action中定义一个成员变量,然后对这个成员变量提供get/set方法,在JSP页...
public void setMessage(String message) {
this.message =
} 2)在JSP页面...……
在Struts2中jsp前台传值到action后台,除了利用javabean还可以... ……
struts2中的Action接收表单传递过来的参数有3种方法:
如,登陆表单login.jsp:
&for... s:submit value="提交"/&
这种方法,在struts开发...……
在Struts2中jsp前台传值到action后台,除了利用javabean还可以... ……
struts2中的Action接收表单传递过来的参数有3种方法:
如,登陆表单login.jsp:
&for... br/&
&s:submitvalue="提交"/&
这种方法,在struts开发...……
Struts2 从action中传值到jsp页面,无法显示出来,找错!! ……
struts2还需这么传值吗?
把要传的值定义为action的成员变量,为其建立set 、get方法,在... value="book.bookname" /&
另外innerText有浏览器兼容问题,例如火狐...……
java(jsp超链接传值到action中,打印出乱码) …… servlet中也有编码设置语句:
request.setCharacterEncoding("UTF-8");
response.setContentType("text/charset=UTF-8");
String getstr = new...……
struts2 s:hidden传值 ActionContext ServletActionContext ……
struts2的标签让你这么用了?????那你还不如不用struts2
你应该在action定义puserid和college变量需要什么类型就定义成什么类型,并生成getter和setter
然后jsp页面你把标...……
jsp页面java代码中如何获取struts2的返回值 ……清楚action的SUCCESS,ERROR这些返回值的作用不?这些是给struts自己用的,不会传到jsp里。如果你要传到jsp里就只能在action里自己加个String的变量,然后在jsp对这个变量...……
所有问题分类Struts2中,action向Jsp页面传值的问题
[问题点数:200分,结帖人guoyoulei520]
本版专家分:1383
2013年9月 移动平台大版内专家分月排行榜第一2013年8月 移动平台大版内专家分月排行榜第一2013年7月 移动平台大版内专家分月排行榜第一2013年6月 移动平台大版内专家分月排行榜第一
2013年10月 移动开发大版内专家分月排行榜第三
CSDN今日推荐
本版专家分:67990
2016年3月 Java大版内专家分月排行榜第一2016年2月 Java大版内专家分月排行榜第一2015年11月 Java大版内专家分月排行榜第一2015年10月 Java大版内专家分月排行榜第一2014年8月 Java大版内专家分月排行榜第一2014年7月 Java大版内专家分月排行榜第一2014年5月 Java大版内专家分月排行榜第一
2016年1月 Java大版内专家分月排行榜第二2015年12月 Java大版内专家分月排行榜第二2015年8月 Java大版内专家分月排行榜第二2015年3月 Java大版内专家分月排行榜第二2015年1月 Java大版内专家分月排行榜第二2014年12月 Java大版内专家分月排行榜第二2014年11月 Java大版内专家分月排行榜第二2014年6月 Java大版内专家分月排行榜第二2014年4月 Java大版内专家分月排行榜第二2014年1月 Java大版内专家分月排行榜第二2013年11月 Java大版内专家分月排行榜第二
2015年9月 Java大版内专家分月排行榜第三2015年6月 Java大版内专家分月排行榜第三2015年5月 Java大版内专家分月排行榜第三2015年2月 Java大版内专家分月排行榜第三2014年3月 Java大版内专家分月排行榜第三2013年12月 Java大版内专家分月排行榜第三
本版专家分:180
本版专家分:6873
2014年10月 Java大版内专家分月排行榜第一
本版专家分:18461
2015年1月 Java大版内专家分月排行榜第一
2014年10月 Java大版内专家分月排行榜第二2014年9月 Java大版内专家分月排行榜第二2014年8月 Java大版内专家分月排行榜第二
2016年1月 Java大版内专家分月排行榜第三2014年12月 Java大版内专家分月排行榜第三2014年11月 Java大版内专家分月排行榜第三
本版专家分:18461
2015年1月 Java大版内专家分月排行榜第一
2014年10月 Java大版内专家分月排行榜第二2014年9月 Java大版内专家分月排行榜第二2014年8月 Java大版内专家分月排行榜第二
2016年1月 Java大版内专家分月排行榜第三2014年12月 Java大版内专家分月排行榜第三2014年11月 Java大版内专家分月排行榜第三
本版专家分:62
本版专家分:62
本版专家分:4813
2014年10月 Java大版内专家分月排行榜第三
本版专家分:2103
本版专家分:11
本版专家分:1647
2014年11月 扩充话题大版内专家分月排行榜第一
2014年12月 扩充话题大版内专家分月排行榜第二
本版专家分:5
本版专家分:1323
本版专家分:1323
本版专家分:1323
本版专家分:5
本版专家分:16
本版专家分:143
本版专家分:491
本版专家分:149
本版专家分:1383
2013年9月 移动平台大版内专家分月排行榜第一2013年8月 移动平台大版内专家分月排行榜第一2013年7月 移动平台大版内专家分月排行榜第一2013年6月 移动平台大版内专家分月排行榜第一
2013年10月 移动开发大版内专家分月排行榜第三
本版专家分:5
本版专家分:5
本版专家分:491
本版专家分:1383
2013年9月 移动平台大版内专家分月排行榜第一2013年8月 移动平台大版内专家分月排行榜第一2013年7月 移动平台大版内专家分月排行榜第一2013年6月 移动平台大版内专家分月排行榜第一
2013年10月 移动开发大版内专家分月排行榜第三
本版专家分:1383
2013年9月 移动平台大版内专家分月排行榜第一2013年8月 移动平台大版内专家分月排行榜第一2013年7月 移动平台大版内专家分月排行榜第一2013年6月 移动平台大版内专家分月排行榜第一
2013年10月 移动开发大版内专家分月排行榜第三
本版专家分:1383
2013年9月 移动平台大版内专家分月排行榜第一2013年8月 移动平台大版内专家分月排行榜第一2013年7月 移动平台大版内专家分月排行榜第一2013年6月 移动平台大版内专家分月排行榜第一
2013年10月 移动开发大版内专家分月排行榜第三
匿名用户不能发表回复!|struts2中,jsp页面通过ajax访问了action,action怎么返回一个json数据给这个jsp页面,
按时间排序
后台:public class pageAction extends ActionSupport{ private S private S private S
public String execute(){
String result = &&;
String message = &&;
&#47;&#47;创建流
PrintWriter out =
&#47;&#47;创建json对象
JSONObject json = new JSONObject();
cmd = ServletActionContext.getRequest().getParameter(&cmd&);
username = ServletActionContext.getRequest().getParameter(&username&);
password = ServletActionContext.getRequest().getParameter(&password&);
&#47;&#47;System.out.println(&username:&+username+&,password:&+password);
if(&admin&.equals(username) && &admin&.equals(password)){
json.put(&content&, &true&);
json.put(&content&, &输入的账号或密码有误!&);
out.write(json.toString());
return SUCCESS; }
}前台:&script type=&text&#47;javascript&&function checkAnswer() {
&#47;&#47;获得输入的账号和密码的值 var username= document.getElementById(&username&). var password= document.getElementById(&password&).
dataStr = {
checkname : username,
checkpass : password }; jQuery
type : &POST&,
url : &ajax&#47;checkAnswer.action?temp=& + Math.random(),
data : dataStr,
dataType : &json&,
success : function(root) {
if (&true& == root.content) {
jQuery.messager.alert(&操作提示&, &登录成功!&, &info&);
jQuery.messager.alert(&操作提示&, root.content, &info&);
} &&#47;script&试试吧,大体是这样的格式,可能还会有一些小错误,注意下就好! 还要注意下在Structs里配置时:
&action name=&seekcardAction& class=&seekcardAction& method=&returnResult&&
&result type=&json& &#47;&
&#47;&#47;注意返回类型
&&#47;action&
举个例子你就好明白了。例如,首先,你写了一个ExampleBean里面有属性userName和password都是String类型,然后别忘了写他们的setter和getter方法,public void setUserName(String userName)等等。然后,写Action,在Action类中加入成员变量private String uNprivate String uP一定要再写上他们的Setter和Getter方法。public void setUName(String s)等等。然后你在execute方法中创建一个bean对象。如:ExampleBean b = new ExampleBean(this.getUName(),this.getUPwd());就可以创建这个bean对象了。具体传值流程是:首先你访问jsp页面,然后比如jsp页面上有一个登录框,你可以输入用户密码。&s:form action=&你设的Actiong名字&&&s:textfield name=&uName& label=&用户名&&#47;&&s:password name=&uPwd& label=&密码&&#47;&&s:submit value=&Go&&#47;&&&#47;s:form&这里注意的是标签中name属性对应的就是Action里面的值.name叫uName,提交表单后在Action里就会自动调用setUName(&你输入的值&);然后就执行execute方法,你就可以创建bean了。明白过程了吗?如果不会还可以追问。
你需要在classpath中添加struts2-json-plugin.jar
1.建DAO类,在里面将这个实体类赋值。2.建service 类UserService,写方法,从DAO里获取这个实体类。3.在action里private 这个javabean,假设这个javabean 为User4.用this.setUserName(userService.getUser());
感谢您为社区的和谐贡献力量请选择举报类型
经过核实后将会做出处理感谢您为社区和谐做出贡献
确定要取消此次报名,退出该活动?
请输入私信内容:

我要回帖

更多关于 jsp struts hibernate 的文章

 

随机推荐