public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
public User(){
System.out.println("===============");
}
public User(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "user="+this.name;
}
}
<s:property value="new User('name')"/>
public class Test {
public static String NAME = "nametom";
public static String test(){
return "testtom";
}
}
在struts.xml配置文件中加<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<s:property value="@Test@test()"/>
<s:property value="@Test@NAME"/>
public class UserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
public String mm(){
return "mm";
}
}
<s:property value="mm()"/>
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
public User(){
System.out.println("===============");
}
public User(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "user="+this.name;
}
}
public class UserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private User user;
private Cat cat;
@Override
public String execute() throws Exception {
// System.out.println(cat.getFriend().getName());
return ActionSupport.SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
}
<s:property value="user.toString()"/>
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
public User(){
System.out.println("========空的构造方法=必须有======");
}
public User(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class UserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private User user;
@Override
public String execute() throws Exception {
System.out.println(user.getName());
return ActionSupport.SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
<body>
<s:form action="user/tologin.action" method="post">
<s:textarea name="id"></s:textarea>
<s:password name="user.password"></s:password>
<s:textfield name="user.name" label="ddddd" > </s:textfield>
<s:submit value="提交" method="login"></s:submit>
<s:submit value="打印" method="print"></s:submit>
</s:form>
</body>
验证框架的命名规则是Action类名字-action的bean名字-validation.xml,这个验证文件需要放在需要验证的.class文件同一目录下
例如UserAction-tologin-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="id">
<field-validator type="int">
<param name="min">10</param>
<param name="max">20</param>
<message>必须是整数</message>
</field-validator>
</field>
<field name="user.name">
<field-validator type="requiredstring">
<message>name必须填写</message>
</field-validator>
</field>
</validators>
在struts-xml中的配置
<action name="tologin" class="UserAction" >
<result>/WEB-INF/user/list.jsp</result>
<result name="input">/WEB-INF/user/user.jsp</result>
</action>
<body>
<s:form action="user/tologin.action" method="post">
<s:textarea name="id"></s:textarea>
<s:password name="user.password"></s:password>
<s:textfield name="user.name"></s:textfield>
<s:submit value="提交" method="login"></s:submit>
<s:submit value="打印" method="print"></s:submit>
</s:form>
</body>
<action name="tologin" class="UserAction" >
<result>/WEB-INF/user/list.jsp</result>
</action>
public class UserAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private User user;
private String id;
@Override
public String execute() throws Exception {
return ActionSupport.SUCCESS;
}
public String login(){
return ActionSupport.SUCCESS;
}
public String print(){
System.out.println(user.getName());
System.out.println(user.getPassword());
System.out.println(id);
return ActionSupport.SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
<a href="hello?name=1">重定向传参数</a>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="hello"></default-action-ref>
<action name="hello" class="UserAction">
<result type="redirect">/index2.jsp?t=${name}</result>
</action>
</package>
public class UserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
@Override
public String execute() throws Exception {
System.out.println(name);
return ActionSupport.SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<s:property value="#parameters.t"/>
第一种
public class UserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private Map session;
private Map request;
private Map application;
@Override
public String execute() throws Exception {
session = ActionContext.getContext().getSession();
request = (Map) ActionContext.getContext().get("request");
application = ActionContext.getContext().getApplication();
session.put("sk", "sv");
request.put("rk", "rv");
application.put("ak", "av");
System.out.println(name);
return ActionSupport.SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
第二种
public class DepAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
public void setRequest(Map<String, Object> request) {
this.request = request;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
public void setApplication(Map<String, Object> application) {
this.application = application;
}
}
第三种
public class CarAction implements ServletRequestAware{
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
this.application = session.getServletContext();
}
}
第四种
public class HomeAction extends ActionSupport{
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public HomeAction (){
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
}
}
摘要: Struts2常用标签总结
一 介绍
1.Struts2的作用
Struts2标签库提供了主题、模板支持,极大地简化了视图页面的编写,而且,struts2的主题、模板都提供了很好的扩展性。实现了更好的代码复用。Struts2允许在页面中使用自定义组件,这完全能满足项目中页面显示复杂,多变的需求。
Struts2的标签库有一个巨大的改进之处,struts2标签库的标签不依赖于任何...
阅读全文