J2EE学习笔记——Struts2多方法实现(1)
分享于 点击 4090 次 点评:174
J2EE学习笔记——Struts2多方法实现(1)
实现 登陆 验证 和注册 验证在一个 LoginAction 类中:
Login.jsp:
[html] view plaincopy
- <%@ page language="java"import="java.util.*"pageEncoding="utf-8"%>
- <%@ taglib uri="/struts-tags"prefix="s" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <basehrefbasehref="<%=basePath%>">
- <title>Login</title>
- <metahttp-equivmetahttp-equiv="pragma"content="no-cache">
- <metahttp-equivmetahttp-equiv="cache-control"content="no-cache">
- <metahttp-equivmetahttp-equiv="expires"content="0">
- <metahttp-equivmetahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equivmetahttp-equiv="description"content="This is my page">
- </head>
- <body>
- <s:formactions:formaction="loginAction"method="post">
- <s:textfieldnames:textfieldname="username"label="用户名"/><br>
- <s:passwordnames:passwordname="password"label="密码"/><br>
- <inputtypeinputtype="submit"value="登陆"><br>
- </s:form>
- </body>
- </html>
LoginAction
[java] view plaincopy
- package xuyan.com.action;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import xuyan.com.model.User;
- import xuyan.com.model.UserDAO;
- import com.opensymphony.xwork2.ActionSupport;
- import com.opensymphony.xwork2.ModelDriven;
- publicclass LoginAction extends ActionSupport implements ModelDriven<User>{
- /**
- *
- */
- privatestaticfinallong serialVersionUID = 1L;
- User user=new User();
- public User getUser() {
- return user;
- }
- publicvoid setUser(User user) {
- this.user = user;
- }
- private Connection con=null;
- private ResultSet rs=null;
- private PreparedStatement psmt=null;
- /**
- * 用户注册
- *
- */
- public String Login()
- {
- System.out.println(user.getUsername()+"1111");
- System.out.println(user.getPassword()+"1111");
- UserDAO dao=new UserDAO();
- con=dao.getConnection();
- try {
- psmt =con.prepareStatement("insert into userinfo (username,password) values (?,?) ");
- psmt.setString(1, user.getUsername());
- psmt.setString(2, user.getPassword());
- int a=psmt.executeUpdate();
- if(a>0)
- {
- System.out.println(user.getUsername()+"第2222次");
- System.out.println(user.getPassword()+"第2222次");
- return SUCCESS;
- }
- else
- {
- return ERROR;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- return ERROR;
- }
- finally
- {
- if(con != null){
- try {
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if(psmt != null){
- try {
- psmt.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if(rs != null){
- try {
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- @Override
- public String execute() {
- System.out.println(user.getUsername()+"1111");
- System.out.println(user.getPassword()+"1111");
- UserDAO dao=new UserDAO();
- con=dao.getConnection();
- try {
- psmt =con.prepareStatement("select * from userinfo where username=? and password=?");
- psmt.setString(1, user.getUsername());
- psmt.setString(2, user.getPassword());
- rs=psmt.executeQuery();
- if(rs.next())
- {
- System.out.println(user.getUsername()+"第2222次");
- System.out.println(user.getPassword()+"第2222次");
- return SUCCESS;
- }
- else
- {
- return ERROR;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- return ERROR;
- }
- finally
- {
- if(con != null){
- try {
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if(psmt != null){
- try {
- psmt.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if(rs != null){
- try {
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public User getModel() {
- // TODO Auto-generated method stub
- return user;
- }
- }
用户点评