2 qq 35103398 qq_35103398 于 2017.09.12 15:36 提问

struts2 中actiong与jsp不能进行数据交互
User.java:

package cn.itcast.nsfw.user.entity;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {

private String id ;
private String name;
private String dept ;
private String account ;
private String password ;
private String headImg ;

private boolean gender;
private String email;
private String mobile;
private Date birthday;
private String state;
private String memo;


public static String USER_STATE_VALID = "1";
public static String USER_STATE_INVALID = "0";


public User() {
    super();
    // TODO Auto-generated constructor stub
}


public User(String id, String name, String dept, String account,
        String password, String headImg, boolean gender, String email,
        String mobile, Date birthday, String state, String memo) {
    super();
    this.id = id;
    this.name = name;
    this.dept = dept;
    this.account = account;
    this.password = password;
    this.headImg = headImg;
    this.gender = gender;
    this.email = email;
    this.mobile = mobile;
    this.birthday = birthday;
    this.state = state;
    this.memo = memo;
}


public String getId() {
    return id;
}


public void setId(String id) {
    this.id = id;
}


public String getName() {
    return name;
}


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


public String getDept() {
    return dept;
}


public void setDept(String dept) {
    this.dept = dept;
}


public String getAccount() {
    return account;
}


public void setAccount(String account) {
    this.account = account;
}


public String getPassword() {
    return password;
}


public void setPassword(String password) {
    this.password = password;
}


public String getHeadImg() {
    return headImg;
}


public void setHeadImg(String headImg) {
    this.headImg = headImg;
}


public boolean isGender() {
    return gender;
}


public void setGender(boolean gender) {
    this.gender = gender;
}


public String getEmail() {
    return email;
}


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


public String getMobile() {
    return mobile;
}


public void setMobile(String mobile) {
    this.mobile = mobile;
}


public Date getBirthday() {
    return birthday;
}


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


public String getState() {
    return state;
}


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


public String getMemo() {
    return memo;
}


public void setMemo(String memo) {
    this.memo = memo;
}

UserAction.java:

package cn.itcast.nsfw.user.action;

import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;

import cn.itcast.nsfw.user.entity.User;
import cn.itcast.nsfw.user.service.UserService;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

private static final User User = null;
@Resource
private UserService userService;
private List<User> userList;
private User user;

//列表页面 

public String listUI(){

userList = userService.findobjects();


    return "listUI";
}
//跳转新增页面
public String addUI(){
    return "addUI";
    }
//保存新增

public String add(){


    if(user !=null){
        userService.save(user);
}
    return "list";
}
.
  .
  .

}

listUI.jsp

package cn.itcast.nsfw.user.entity;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {

private String id ;
private String name;
private String dept ;
private String account ;
private String password ;
private String headImg ;

private boolean gender;
private String email;
private String mobile;
private Date birthday;
private String state;
private String memo;


public static String USER_STATE_VALID = "1";
public static String USER_STATE_INVALID = "0";


public User() {
    super();
    // TODO Auto-generated constructor stub
}


public User(String id, String name, String dept, String account,
        String password, String headImg, boolean gender, String email,
        String mobile, Date birthday, String state, String memo) {
    super();
    this.id = id;
    this.name = name;
    this.dept = dept;
    this.account = account;
    this.password = password;
    this.headImg = headImg;
    this.gender = gender;
    this.email = email;
    this.mobile = mobile;
    this.birthday = birthday;
    this.state = state;
    this.memo = memo;
}


public String getId() {
    return id;
}


public void setId(String id) {
    this.id = id;
}


public String getName() {
    return name;
}


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


public String getDept() {
    return dept;
}


public void setDept(String dept) {
    this.dept = dept;
}


public String getAccount() {
    return account;
}


public void setAccount(String account) {
    this.account = account;
}


public String getPassword() {
    return password;
}


public void setPassword(String password) {
    this.password = password;
}


public String getHeadImg() {
    return headImg;
}


public void setHeadImg(String headImg) {
    this.headImg = headImg;
}


public boolean isGender() {
    return gender;
}


public void setGender(boolean gender) {
    this.gender = gender;
}


public String getEmail() {
    return email;
}


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


public String getMobile() {
    return mobile;
}


public void setMobile(String mobile) {
    this.mobile = mobile;
}


public Date getBirthday() {
    return birthday;
}


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


public String getState() {
    return state;
}


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


public String getMemo() {
    return memo;
}


public void setMemo(String memo) {
    this.memo = memo;
}

}

addUI.jsp:

package cn.itcast.nsfw.user.entity;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {

private String id ;
private String name;
private String dept ;
private String account ;
private String password ;
private String headImg ;

private boolean gender;
private String email;
private String mobile;
private Date birthday;
private String state;
private String memo;


public static String USER_STATE_VALID = "1";
public static String USER_STATE_INVALID = "0";


public User() {
    super();
    // TODO Auto-generated constructor stub
}


public User(String id, String name, String dept, String account,
        String password, String headImg, boolean gender, String email,
        String mobile, Date birthday, String state, String memo) {
    super();
    this.id = id;
    this.name = name;
    this.dept = dept;
    this.account = account;
    this.password = password;
    this.headImg = headImg;
    this.gender = gender;
    this.email = email;
    this.mobile = mobile;
    this.birthday = birthday;
    this.state = state;
    this.memo = memo;
}


public String getId() {
    return id;
}


public void setId(String id) {
    this.id = id;
}


public String getName() {
    return name;
}


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


public String getDept() {
    return dept;
}


public void setDept(String dept) {
    this.dept = dept;
}


public String getAccount() {
    return account;
}


public void setAccount(String account) {
    this.account = account;
}


public String getPassword() {
    return password;
}


public void setPassword(String password) {
    this.password = password;
}


public String getHeadImg() {
    return headImg;
}


public void setHeadImg(String headImg) {
    this.headImg = headImg;
}


public boolean isGender() {
    return gender;
}


public void setGender(boolean gender) {
    this.gender = gender;
}


public String getEmail() {
    return email;
}


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


public String getMobile() {
    return mobile;
}


public void setMobile(String mobile) {
    this.mobile = mobile;
}


public Date getBirthday() {
    return birthday;
}


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


public String getState() {
    return state;
}


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


public String getMemo() {
    return memo;
}


public void setMemo(String memo) {
    this.memo = memo;
}

}

user-struts.xml
<?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">

<package name="user" namespace="/nsfw" extends="struts-default">
    <action name="user_*" class="cn.itcast.nsfw.user.action.UserAction" method="{1}">
        <result name="{1}">/WEB-INF/jsp/nsfw/user/{1}.jsp</result>
        <result name="list" type="redirectAction">
              <param name="actionName">user_listUI</param>
              <param name="user.name">${strName}</param>
              <param name="encode">true</param>
        </result>
    </action>
</package>

问题一:action 的数据不能传到jsp
浏览器输入http://localhost:8080/itcastTax/nsfw/user_listUI.action访问listUI.jsp的结果如下:
图片说明
1. 不能将数据库的user显示出来
2.从Debug可以看到action已经从数据库的数据取到listuser
图片说明

问题二:jsp的数据不能传到action
在listUI.jsp点击新建进入addUI.jsp,输入数据点击保存,在数据库的user并没有添加。
图片说明

1个回答

bingyan_wxl
bingyan_wxl   2017.09.12 15:43

你贴的jsp文件的代码好像是User类的代码吧

Csdn user default icon
上传中...
上传图片
插入图片