这个登录注册做了能有3天吧,遇到了几个问题,耽误了些时间。现在把问题记下来,防止以后忘掉。也希望对大家有所帮助吧。代码如下:
struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="my" extends="struts-default" namespace="/" >
<action name="LoginAction" class="com.struts.action.LoginAction">
<result name="login">/result.jsp</result>
<result name="regist">/result.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
login.jsp
<%@ page language="java" contentType="text/html; charset=GB2312" pageEncoding="GB2312" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<s:form action="LoginAction" method="post" namespace="/" >
<s:label value="登录"></s:label>
<s:textfield name="userName" label="请输入用户名"></s:textfield>
<s:password name="password" label="请输入密码"></s:password>
<s:submit value="登录" align="left" method="login"></s:submit>
<s:submit value="注册" align="left" method="regist"></s:submit>
</s:form>
</body>
</html>
result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>提交结果</title>
</head>
<body>
<h1>${result}</h1>
</body>
</html>
LoginAction.java
package com.struts.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
import com.struts.service.LoginService;
@SuppressWarnings("serial")
public class LoginAction extends ActionSupport implements ServletRequestAware {
// 用户名
private String userName;
// 用户密码
private String password;
// Service
private LoginService service = new LoginService();
// 获得HttpServletRequest对象
private javax.servlet.http.HttpServletRequest request;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
/**
* 登录
* @return
*/
public String login() throws Exception {
// 登录用户check
boolean flg = service.userLogin(userName, password);
if (flg) {
request.setAttribute("result", "成功保存[" + "登录成功" + "]");
return "login";
}
request.setAttribute("result", "成功保存[" + "登录失败" + "]");
return "login";
}
/**
* 注册
* @return
*/
public String regist() throws Exception {
// 用户注册
boolean flg = service.userRegist(userName, password);
if (flg) {
request.setAttribute("result", "成功保存[" + "注册成功" + "]");
return "regist";
}
request.setAttribute("result", "成功保存[" + "注册失败" + "]");
return "regist";
}
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;
}
}
LoginService.java
package com.struts.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class LoginService {
Connection con = null;
Statement stat = null;
ResultSet rs = null;
public LoginService() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
stat = con.createStatement();
} catch (Exception e) {
con = null;
}
}
/**
* 登录用户check
*
* @param userName
* @param passWord
*/
public boolean userLogin(String userName, String passWord) {
String sql = "select * from tb_user where username='" + userName+"' and password ='"+passWord+"'";
try {
rs = executeQuery(sql);
if (rs.next()) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 数据库查询
*
* @param userName
* @param passWord
*/
public ResultSet executeQuery(String sql) {
try {
rs = stat.executeQuery(sql);
} catch (Exception e) {
rs = null;
}
return rs;
}
public boolean userRegist(String userName, String passWord) {
String sql = "insert into tb_user(username,password) values('"+userName+"','"+passWord+"')";
try {
stat.executeUpdate(sql);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
大致能跑的,就是跑数据库的时候有点问题,没那么多时间去研究了~
其实一直困扰的我的问题就是一个form里面有多个submit按钮时要怎么跳转到同一个action里面的不同的方法。
网上这种例子有很多,但大多数都实现不了。后来不经意看到一个论坛上一个帖子的一个神回复。于是试了一下果然成功了。
主要就是在struts.xml中加一句话,在上述代码中已经写出来了
就是这句话。
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
value必须是true,否则就不能动态的分配了。
就总结到这里了。希望大家相互指正,相互学习~