本文主要是介绍7.10--SSH学习之Struts2 Action配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在说struts2之前当然要先下载好其框架了,官网有推荐
压缩包含:
apps:使用struts开发的一些demo
src:一个示例
docs:文档
lib:jar包
三种创建Action的方式
- 创建普通类,编写execute()方法
- 创建Action类,实现Action接口
- 创建Action类,继承ActionSupport类
示例一:
public class FirstAction {public String execute()throws Exception{System.out.println("in FirstAction method execute()");return "success";}}
示例二:
public class SecondAction implements Action {@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubSystem.out.println("in SecondAction method execute()");return SUCCESS;}}
示例三:
public class ThirdAction extends ActionSupport {@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubSystem.out.println("in ThirdAction method execute()");return "success";}}
三种调用Action方法的方式
- 调用execute()方法响应客户端请求
- 动态方法调用
- 调用指定名字的方法响应客户端请求, 一个Action 类可包含多个方法,最好是3-5个
struts.xml
<!-- 动态方法盗用 --><constant name="struts.enable.DynamicMethodInvocation" value="true" /><constant name="struts.devMode" value="true" /><package name="default" namespace="/" extends="struts-default"><!-- 第一种调用方式,execute()方法,如后面有method="",则不调用默认execute().而调用method指定的方法 --><action name="first" class="com.su.web.action.FirstAction"><result type="dispatcher" name="success">/success.jsp</result></action><action name="stu" class="com.su.web.action.StudentAction"><result type="dispatcher" name="success">/success.jsp</result></action><!-- 第二种调用方式,动态调用 stu!addStudent,调用StudentAction类中的addStudent方法--><action name="addStu" class="com.su.web.action.StudentAction" method="addStudent"><result type="dispatcher" name="success">/success.jsp</result></action><action name="updateStu" class="com.su.web.action.StudentAction" method="updateStudent"><result type="dispatcher" name="success">/success.jsp</result></action><action name="deleteStu" class="com.su.web.action.StudentAction" method="deleteStudent"><result type="dispatcher" name="success">/success.jsp</result></action><!-- 第三种调用方式,调用指定的方法 ,stu_addStudent,后面的{1}中为addStudent,也称为占位符调用--><action name="stu_*" class="com.su.web.action.StudentAction" method="{1}"><result type="dispatcher" name="success">/success_{1}.jsp</result></action><!-- 后缀有两个时,可往后加<action name="stu_*_*" class="com.su.web.action.StudentAction" method="{1}_{2}"><result type="dispatcher" name="success">/success_{1}_{2}.jsp</result></action>-->
示例一:
<form action="first" method="post"><input type="submit" value="提交"></form>
示例二:
<form action="stu!addStudent" method="post"><input type="submit" value="提交"></form>
示例三:
form action="stu_addStudent" method="post"><input type="submit" value="提交"></form>
三种接收表单数据的方式
- Action的普通属性传参
- Action对象属性传参
- ModekDriven传参(缺点是只能由一个实体)
示例一:
public class UserAction extends ActionSupport {//普通属性传参private String userName;private String userPwd;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getUserPwd() {return userPwd;}public void setUserPwd(String userPwd) {this.userPwd = userPwd;}public String execute() throws Exception{System.out.println("in execute!!!");System.out.println(userName);System.out.println(userPwd);return "success";}}
示例二:
public class UserTwoAction extends ActionSupport{//对象属性传参private User user;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public String execute() throws Exception{System.out.println("in execute!!!");System.out.println(user.getUserName());System.out.println(user.getUserPwd());ActionContext.getContext().put("message", "!!!密码或用户名错误!!!");return "success";}public String userlogin() throws Exception{System.out.println("in userlogin!!!");System.out.println(user.getUserName());System.out.println(user.getUserPwd());if(user.getUserName().equals("susu") && user.getUserPwd().equals("1111")){ActionContext.getContext().put("message", "!!!登录成功!!!");return "success";}return "nextAction";}
}
示例三:
//ModelDriven传参private User user= new User();public User getUser() {return user;}public void setUser(User user) {this.user = user;}//@Overridepublic User getModel() {// TODO Auto-generated method stubreturn user;}public String userlogin() throws Exception{System.out.println("in userlogin!!!");System.out.println(user.getUserName());System.out.println(user.getUserPwd());return "success";}}
表单JSP页面
<body><form action="user_userlogin" method="post"><!-- 普通属性传参 --><!-- 用户名:<input type="text" name="userName">密 码:<input type="password" name="userPwd"><input type="submit" value="登录">--><!-- 对象属性传参,表单元素的名字就是:Action的属性名.Action属性的属性名 --><!-- 用户名:<input type="text" name="user.userName">密 码:<input type="password" name="user.userPwd"><input type="submit" value="登录">-->用户名:<input type="text" name="userName">密 码:<input type="password" name="userPwd"><input type="submit" value="登录"></form></body>
四种Action请求下一个资源的方法
- dispatcher转发,Action To JSP
- chain转发,Action To Action
- redirect重定向,Action To JSP
- redirectAction 重定向,Action To Action
PS:转发:在原JSP页面提交的用户名和密码之类的信息不丢失
重定向:则丢失
示例一:
<action name="userTwo" class="com.su.web.action.UserTwoAction" ><result type="dispatcher" name="success">/indexRedict.jsp</result></action>
示例二:
<action name="userTwo_*" class="com.su.web.action.UserTwoAction" method="{1}"><result type="chain" name="nextAction">userTwo</result><result type="dispatcher" name="success">/success_user.jsp</result></action>
示例三:
<action name="userTwo_*" class="com.su.web.action.UserTwoAction" method="{1}"><result type="redirectAction" name="nextAction">userTwo</result><result type="redirect" name="success">/success_user.jsp</result></action>
Author:su1573
这篇关于7.10--SSH学习之Struts2 Action配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!