本篇接连曾经发表的《简单的struts2框架(一)》,在次基础上有所增加
action可以接收多个请求:
1 package action; 2 import java.util.Date; 3 import service.UserService; 4 import com.opensymphony.xwork2.ActionSupport; 5 import entity.Users; 6 public class UserAction extends ActionSupport { 7 private String username; 8 private String userpwd; 9 private Date birthDate; 10 private UserService userservice=new UserService(); 11 /** 12 * 默认情况下执行execute 13 * @return 14 */ 15 public String doLogin(){ 16 //业务功能的调用 17 System.out.println(username+userpwd); 18 boolean ret=userservice.login(username, userpwd); 19 if(ret){ 20 //呈现数据,给struts.xml的result 21 return "success"; 22 }else{ 23 return "error"; 24 } 25 } 26 /** 27 * 用户注册 28 * @return 29 */ 30 public String doRegister(){ 31 Users user=new Users(1, username, userpwd,birthDate); 32 boolean ret=userservice.register(user); 33 if(ret){ 34 //呈现数据,给struts.xml的result 35 return "reg_success"; 36 }else{ 37 return "reg_error"; 38 } 39 } 40 public String getUsername() { 41 return username; 42 } 43 public void setUsername(String username) { 44 this.username = username; 45 } 46 public String getUserpwd() { 47 return userpwd; 48 } 49 public void setUserpwd(String userpwd) { 50 this.userpwd = userpwd; 51 } 52 }
这时候的UserService为:
1 package service; 2 import java.sql.Date; 3 import dao.JDBC; 4 import entity.Users; 5 public class UserService { 6 7 public boolean login(String username,String userpwd){ 8 if(username.equals("111")&&userpwd.equals("111")){ 9 return true; 10 }else{ 11 return false; 12 } 13 } 14 15 /* 16 * 用户注册 17 */ 18 public boolean register(Users user){ 19 //jdbc的代码 20 String sql="insert into users(userName,userPwd,birthDate) values (?,?,?)"; 21 22 java.sql.Date birthDate=new java.sql.Date(user.getBirthDate().getTime()); 23 24 Object[] params={ 25 user.getUserName(), 26 user.getUserPwd(), 27 birthDate 28 }; 29 int num=JDBC.execUpdate(sql,params); 30 if(num>0){ 31 return true; 32 }else{ 33 return false; 34 } 35 } 36 }
当这些处理完后,struts.xml的修改为:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 7 <package name="default" namespace="/user" extends="struts-default"> 8 <action name="loginAction" class="action.UserAction" method="doLogin"> 9 <result name="success" type="dispatcher">/user/Success.jsp</result> 10 <result name="error" type="dispatcher">/user/Error.jsp</result> 11 </action> 12 13 <action name="registerAction" class="action.UserAction" method="doRegister"> 14 <result name="reg_success" 15 16 type="dispatcher">/user/UserLogin.jsp</result> 17 <result name="reg_error" type="dispatcher">/user/Error.jsp</result> 18 </action> 19 20 </package> 21 22 </struts>
从代码的改变可以看出:<action name="loginAction" class="action.UserAction" method="doLogin">多了method属性,如果不添加,就如上篇博文里的代码,走的是默认的方法:execute,而定义了method会执行 action里相对应的方法
当然在jsp页面,也需要调用struts.xml相对应的action名,如:
1 <form action="/practice_struts2/user/registerAction.action" method="post"> 2 3 用户名:<input name="username" type="text"><br/> 4 密 码:<input name="userpwd" type="password"><br/> 5 出生日期:<input name="birthDate" type="text">yyyy-MM-dd<br/> 6 <input type="submit" value="注册"/><input type="reset" value="重置"/> 7 </form>
这里相对于servlet有一个方便之处,就是Date,只需写成对应的yyyy-MM-dd,action会自动转换为这样格式的 Date值