大侠、高手请进!!
index.jsp代码:
<%@ page language = "java" import = "java.util.*" pageEncoding ="GB18030"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<html>
<head>
<title>用户注册</title>
</head>
<body>
<s:form action="user1.action" method="post">
id:<s:textfield name="user.name"></s:textfield></br>
pw:<s:password name="user.password"></s:password>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
struts.xml代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="user" class ="com.bjsxt.hibernate.action.UserAction">
<result name="success">
/registerSuccess.jsp
</result>
<result name="fail">registerFail.jsp</result>
</action>
</struts>
UserAction.java代码:
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import com.bjsxt.hibernate.model.User;
import com.bjsxt.hibernate.service.UserManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class User1Action extends ActionSupport{
private User user ;
private UserManager um ;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserManager getUm() {
return um;
}
public void setUm(UserManager um) {
this.um = um;
}
@Override
public String execute()throws Exception{
user = new User();
um = new UserManager();
user.setName("b");
user.setPassword("b");
if(um.exists(user)){
return "fail";
}
um.add(user);
return "success";
}
}
registerSuccess.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding ="GB18030"%>
<html>
<head>
<title></title>
</head>
<body>
<h1>success!!!</h1>
</body>
</html>
registerFail.jsp
<%@ page language = "java" import = "java.util.*" pageEncoding ="GB18030"%>
<html>
<head>
<title></title>
</head>
<body>
<h1>fail!!!</h1>
</body>
</html>
User.java代码:
package com.bjsxt.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User
{
private int id ;
private String name;
private String password;
public User(){}
public void setId(int id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Id
@GeneratedValue
public int getId(){
return id;
}
public String getName(){
return name;
}
}
UserManager.java代码:
package com.bjsxt.hibernate.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.bjsxt.hibernate.model.User;
public class UserManager {
public boolean exists(User u)throws Exception{
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/spring" , "root" ,
"mysqladmin");
String sqlQuery = "select count(*)from user where name=?";
PreparedStatement psQuery = conn.prepareStatement(sqlQuery);
psQuery.setString(1,u.getName());
ResultSet rs = psQuery.executeQuery();
rs.next();
int count = rs.getInt(1);
conn.close();
if(count > 0){
return true;
}
return false;
}
public void add(User u)throws Exception{
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/spring" , "root" ,
"mysqladmin");
String sql = "insert into user value (null,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,u.getName());
ps.setString(2,u.getPassword());
ps.executeUpdate();
ps.close();
conn.close();
}
}
现在的问题是:我按index.jsp页面中提交按钮,就会转执行UserAction类中的execute方法,execute方法内new了一个
User对象user,把属性name、password都设成了b,第一次能成功把“b”存到数据库中。第二次在index.jsp表单中输入任
何数据提交,因为user的name、password是设成定死的“b”,无论提交什么数据,user的name、password永远都是“b”。
这样,也就是第一次提交能“success”,以后都是“fail”。有谁可以帮帮小弟呀!!搞了两天还没明白。
[解决办法]
user.setName("b");
user.setPassword("b");
你自己设死了这个值当然不会变啦
[解决办法]
楼主,建议好好下个strutsApi好好学习一下吧,直接在action类中获取user对象不就行了
干嘛要new 一个user啊 直接获取页面上的user 在set进去不就行了
[解决办法]
action中只要有private User user 和它的get、set方法就可以直接获取到前台传过来的值,不用new对象,struts内部会进行处理
[解决办法]
//有ActionForm没有,没有新建一个ActionForm 里面放实体类 get set 方法 然后在Action里用
user = ActionForm.getUser()//这个就是你页面上的user对象
[解决办法]
struts2 的Action有bean对象,jsp使用struts标签,ognl可以直接获取bean中的属性或方法!
[解决办法]