1.编写Javabean
/src/myuser/User.java
在strut1中,Javabean需要继承于struts1 api中的ActionForm类。struts2没有此要求
struts2 也没有formbean,通常使用Domain Model的方式向action注入数据
1 package myuser; 2 3 public class User { 4 private int id; 5 private String userName; 6 private String pwd; 7 public int getId() { 8 return id; 9 } 10 public void setId(int id) { 11 this.id = id; 12 } 13 public String getUserName() { 14 return userName; 15 } 16 public void setUserName(String userName) { 17 this.userName = userName; 18 } 19 public String getPwd() { 20 return pwd; 21 } 22 public void setPwd(String pwd) { 23 this.pwd = pwd; 24 } 25 26 }
2.编写DAO类
/src/myuser/UserDAO.java
package myuser; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import db.DbLib; public class UserDAO { private Connection conn; PreparedStatement ps; public String sql; /** * 通过JNDI的方式连接数据库 * @throws ClassNotFoundException * @throws SQLException * @throws NamingException */ public UserDAO() throws ClassNotFoundException, SQLException, NamingException { //JNDI方式 Context ctx = new InitialContext(); //获取数据源 DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql"); //通过数据源取连接池中的连接 conn = ds.getConnection(); } /** * 用户登录 * @param user * @return * @throws SQLException */ public boolean checkLogin(User user) throws SQLException { boolean flag=false; sql="select * from users where userName=? and pwd=?"; ps=conn.prepareStatement(sql); ps.setString(1, user.getUserName()); ps.setString(2, user.getPwd()); ResultSet rs=ps.executeQuery(); if(rs.next()) { flag=true; } conn.close(); System.out.println("登录成功"); return flag; }
3.编写Action
/src/myuser/UserAction.java
在struts1, action必须继承于struts1 api 中Action。struts2,没有继承类或实现接口的要求,随便写一个类,都可以做struts2 的action
action中的处理请求的方法的签名是有要求
格式:public String execute()
package myuser; import java.io.PrintWriter; import java.sql.SQLException; import java.util.ArrayList; import javax.naming.NamingException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import mypicture.Picture; import mypicture.PictureDAO; /** * 为了方便继承ActionSupport */ public class UserAction extends ActionSupport { /** * action运行的时候的servlet环境 * 获取response */ HttpServletResponse response=ServletActionContext.getResponse(); /** * 获取request,再获取session */ HttpServletRequest request=ServletActionContext.getRequest(); HttpSession session=request.getSession(); PrintWriter out; /** * 向action注入数据的方式,通常使用Domain Model的方式 */ private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } /** * 用户登录 * @return * @throws Exception */ public String checkLogin() throws Exception { out=response.getWriter(); UserDAO dao=new UserDAO(); if(!dao.checkLogin(user)) { out.print("0"); } //把user存入session中 session.setAttribute("user", user); out.print("1"); //不跳转 return null; }
4.配置Action
/src/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"> <struts> <!-- struts2常量 --> <!-- 控制请求action时的后缀,默认为空或者.action --> <constant name="struts.action.extension" value="action,,"/> <!-- 控制开发模式,默认为false,若为true则表示改了action后可以不重启--> <constant name="struts.devMode" value="true"/> <!-- 控制上传文件大小 3个0表示Kb,6个0表示Mb,这里为10Mb--> <constant name="struts.multipart.maxSize" value="10000000"/> <!-- package:管理action name:名称,今后别的人可以继承这个package namespace:路径,调action的时候也要加上才能调用 extends:继承于哪个包,这里继承于struts-default包(实现了很多struts功能) --> <package name="all" namespace="" extends="struts-default"> <!-- 控制action中全局跳转 --> <global-results> <!-- redirectAction容器外跳转,跳action --> <result name="userlist" type="redirectAction"> <param name="namespace">/user</param> <param name="actionName">list</param> </result> </global-results> </package> <include file="user.xml"></include> <include file="picture.xml"></include> </struts>
/src/user.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"> <struts> <package name="user" namespace="/user" extends="all"> <!-- 通配符方式配置action,解决action太多的问题 --> <action name="*" class="myuser.UserAction" method="{1}"> <!-- 控制action中局部跳转 --> <result name="main">${pageContext.request.contextPath}/main.jsp</result> <result name="login">${pageContext.request.contextPath}/login.jsp</result> <!-- 加了type="redirect"代表,容器外跳转 容器外跳转跳不到userlist.jsp中去 在这里只能用容器内跳转(同一个request范围之内) --> <result name="list">/WEB-INF/user/userlist.jsp</result> <result name="modify">/WEB-INF/user/modify.jsp</result> </action> </package> </struts>
原文地址:https://www.cnblogs.com/zizi-tothemoon/p/9297832.html
时间: 2024-10-11 18:24:47