这个是 struts.xml 配置文件:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts" extends="struts-default">
<action name="login" class="database.operation.LoginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>这个是web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>这个是loginAction:
package database.operation;import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;
import java.util.List;
import vo.User;
import vo.HibernateSessionFactory;import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
protected String username;
protected String password;
protected SessionFactory sessionFactory;
protected User user;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public SessionFactory getSessionFactory(){
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory=sessionFactory;
}
//处理用户请求的execute方法 用户注册
public String execute() throws Exception{
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
session.save(user);
tx.commit();
session.close(); if(getUsername().equals("")&& getPassword().equals("")){
return "error";
}
else{
return "success";
}
}
}
页面信息:
<%@ page contentType="text/html;charset=gb2312" %>
<HTML><BODY><FONT size=3>
<form action="login.action" method="post">
注册账号:<input type="text" name="username"/><BR>
注册密码:<input type="password" name="password"/><BR>
<input type="submit" value="注册">
</form></FONT></BODY></HTML>
返回:<%@ page contentType="text/html;charset=gb2312" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<HTML>
<BODY><s:property value="username"/>注册成功
</BODY></HTML><%@ page contentType="text/html;charset=gb2312" %>
<HTML><BODY>
注册失败
</BODY></HTML>
所用到的数据库是 mysql 表是 user 有 username 和 password 就是 数据写不到数据库里!!!
谢谢了 帮忙 改改呗!!!!
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts" extends="struts-default">
<action name="login" class="database.operation.LoginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>这个是web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>这个是loginAction:
package database.operation;import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;
import java.util.List;
import vo.User;
import vo.HibernateSessionFactory;import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
protected String username;
protected String password;
protected SessionFactory sessionFactory;
protected User user;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public SessionFactory getSessionFactory(){
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory=sessionFactory;
}
//处理用户请求的execute方法 用户注册
public String execute() throws Exception{
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
session.save(user);
tx.commit();
session.close(); if(getUsername().equals("")&& getPassword().equals("")){
return "error";
}
else{
return "success";
}
}
}
页面信息:
<%@ page contentType="text/html;charset=gb2312" %>
<HTML><BODY><FONT size=3>
<form action="login.action" method="post">
注册账号:<input type="text" name="username"/><BR>
注册密码:<input type="password" name="password"/><BR>
<input type="submit" value="注册">
</form></FONT></BODY></HTML>
返回:<%@ page contentType="text/html;charset=gb2312" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<HTML>
<BODY><s:property value="username"/>注册成功
</BODY></HTML><%@ page contentType="text/html;charset=gb2312" %>
<HTML><BODY>
注册失败
</BODY></HTML>
所用到的数据库是 mysql 表是 user 有 username 和 password 就是 数据写不到数据库里!!!
谢谢了 帮忙 改改呗!!!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货