<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!--
不推荐使用
<package name="default" namespace="/hwq" extends="struts-default">
<action name="hello" class="com.st2.test.HelloWord" method="add">
<result name="addSuccess">/hello.jsp</result>
</action>
</package>
-->
<!-- 推荐使用
<package name="default" namespace="/actions" extends="struts-default">
<action name="student*" class="com.st2.test.StudentAction" method="{1}">
<result>/student{1}_success.jsp</result>
</action>
</package>
-->
<!-- 最简单的配置 -->
<package name="actions" namespace="/actions" extends="struts-default">
<!--<default-action-ref name="index"></default-action-ref>-->
<action name="*_*" class="com.st2.test.{1}Action" method="{2}">
<result>/{1}_{2}_success.jsp</result>
<result name="error">/stu_delete_error.jsp</result>
</action>
</package>
</struts>
class:
package com.st2.test;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import com.st2.test.mode.Student;
public class StudentAction extends ActionSupport implements RequestAware,SessionAware, ApplicationAware
{
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String add()
{
return SUCCESS;
}
public String delete()
{
System.out.println("name="+student.getName());
System.out.println("age="+student.getAge());
if(student.getName() == null || !student.getName().equals("hwq"))
{
this.addFieldError("deleteError", "go bed error!");
this.addFieldError("deleteError", "go bed is not happy!");
request.put("hwq", "1012");
return "error";
}
return SUCCESS;
}
//初始化
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;
}
}