<package name="front" namespace="/" extends="struts-default"/> 和java一样 是打包
<result name="success"/> 凡是name="success“ 可以不写,因为默认就是success
当copy一个struts 项目还需要改动 把原来的项目名称改成现在的
namespace决定了action的访问路径,默认为"",可以接收所有路径的action
namespace可以写为/,或者/xxx,或者/xxx/yyy,对应的action访问路径为/index.action,/xxx/index.action,或者/xxx/yyy/index.action.
namespace最好也用模块来进行命名
定义自己的Action
D:\dongxing\SSH\struts\res\xwork-2.1.2\src\java引入方法与struts一样xwork 的源码
xwork --> java Source Attachment ---> D:\dongxing\SSH\struts\res\xwork-2.1.2\src\java按 F3 可以切换到下一个联系的属性、方法、类、
package com.demo; public class IndexAction1 { public String execute() { return "success"; } }
package com.demo; import com.opensymphony.xwork2.Action; public class IndexAction2 implements Action{ public String execute() throws Exception { return "success" ; } }
开发的时候只用ActionSupport
package com.demo; import com.opensymphony.xwork2.ActionSupport; public class IndexAction3 extends ActionSupport { @Override public String execute() throws Exception { return "success" ; } }
具体视图的返回可以由用户自己定义的Action来决定
具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容
具体Action的实现可以是一个普通的java类,里面有public String execute方法即可
或者实现Action接口
不过最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法<constant name="struts.devMode" value="true"/> <package name="front" extends="struts-default" namespace="/"> <action name="index" class="com.demo.IndexAction3"> <result name="success"> /ActionIntroduction.jsp </result> </action> </package>
路径设置
struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。
index.jsp
虽然可以用redirect方式解决,但redirect方式并非必要。
解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)
或者使用myeclipse经常用的,指定basePathpackage com.demo; public class PathAction { public String execute() { return "path" ; } }
<constant name="struts.devMode" value="true"/> <package name="front" extends="struts-default" namespace="/path"> <action name="path" class="com.demo.PathAction"> <result name="path"> /path.jsp </result> </action> </package>
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <body> struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。<br /> <a href="<%=basePath%>index.jsp">index.jsp</a> <br /> 虽然可以用redirect方式解决,但redirect方式并非必要。 <br /> 解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径) <br /> 或者使用myeclipse经常用的,指定basePath </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <html> <head> </head> <body> <a href="path/path.action">路径问题说明</a> </body> </html>
String path = request.getContextPath(); webapplication 就是项目名称<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
String basePath = request.getScheme() http
+"://"+
request.getServerName() localhost
+":"+
request.getServerPort() 8080
+path 就是上面的项目名称
+"/";
相当于 http://localhost:8080/Struts2_4_Introduction/
即绝对路径
package com.demo; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { public String add() { return SUCCESS ; } }
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String context = request.getContextPath(); %> <html> <head> </head> <body> Action执行的时候并不一定要执行execute方法<br /> 可以在配置文件中配置Action的时候用method=来指定执行哪个方法 也可以在url地址中动态指定(动态方法调用DMI)(推荐)<br /> <a href="<%=context %>/user/userAdd">添加用户</a> <br /> <a href="<%=context %>/user/user!add">添加用户</a> <br /> 前者会产生太多的action,所以不推荐使用 </body> </html>
开发中最常用以下这种<?xml version="1.0" encoding="UTF-8" ?> <!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.devMode" value="true"/> <package name="front" extends="struts-default" namespace="/user"> <action name="userAdd" class="com.demo.UserAction" method="add"> <result> /user_add.success.jsp </result> </action> <action name="user" class="com.demo.UserAction"> <result> /user_add_success.jsp </result> </action> </package> </struts>
</action> <action name="user" class="com.demo.UserAction"> <result> /user_add_success.jsp </result> </action>
通配符
使用通配符,将配置降到最低
添加学生 删除学生
不过,一定要遵守“约定优于配置”的原则
添加老师 删除老师 添加课程 删除课程package com.demo; import com.opensymphony.xwork2.ActionSupport; public class StudentAction extends ActionSupport { public String add() { return SUCCESS; } public String delete() { return SUCCESS; } }
package com.demo; import com.opensymphony.xwork2.ActionSupport; public class TeacherAction extends ActionSupport{ public String add() { return SUCCESS; } public String delete() { return SUCCESS; } }
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> </head> <body> 使用通配符,将配置降到最低 <br/> <a href="<%=path%>/actions/Studentadd">添加学生</a> <a href="<%=path%>/actions/Studentdelete">删除学生</a> <br/> 不过,一定要遵守“约定优于配置”的原则 <br/> <a href="<%=path %>/actions/Teacher_add">添加老师</a> <a href="<%=path %>/actions/Teacher_delete">删除老师</a> <a href="<%=path %>/actions/Course_add">添加课程</a> <a href="<%=path %>/actions/Course_delete">删除课程</a> </body> </html>
<?xml version="1.0" encoding="UTF-8" ?> <!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.devMode" value="true"/> <package name="front" extends="struts-default" namespace="/actions"> <action name="Student*" class="com.demo.StudentAction" method="{1}"> <result> /Student{1}_success.jsp </result> </action> <action name="*_*" class="com.demo.{1}Action" method="{2}"> <result> /{1}_{2}_success.jsp </result> </action> </package> </struts>
<action name="Student*" class="com.demo.StudentAction" method="{1}"> ===> {1} 代表第一个* 号
首先匹配最精确、前后顺序
action 传参数
package com.demo; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private String name; private int age; public String add() { System.out.println("name= " + name); System.out.println("age= " + age); return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
<body> 使用action 属性接收参数 <a href="user/user!add?name=a&age=9">添加用户</a> </body>
<constant name="struts.devMode" value="true"/> <package name="front" extends="struts-default" namespace="/user"> <action name="user" class="com.demo.UserAction"> <result> /user_add_success.jsp </result> </action> </package>
DTO
package com.demo.action; import com.demo.dto.UserDTO; import com.demo.model.User; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private User user; //private UserDTO userDTO; public String add() { System.out.println("name= " + user.getName()); System.out.println("age= " + user.getAge()); return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
使用Dmain Model 属性接收参数 <a href="user/user!add?user.name=a&user.age=9">添加用户(最常用接收参数的方法)</a>
user.属性接收参数
package com.demo.dto; public class UserDTO { private String name; private String password; private String confirmingPassword; }
ModelDrivenParamInput
package com.demo.action; import com.demo.model.User; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class UserAction extends ActionSupport implements ModelDriven<User> { private User user = new User(); public String add() { System.out.println("name= " + user.getName()); System.out.println("age= " + user.getAge()); return SUCCESS; } public User getModel() { return user; } } package com.demo.model; public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } index.jsp 使用Dmain Model 属性接收参数 <a href="user/user!add?name=a&age=9">添加用户</a>