struts2配置和用发

 

<?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;   

    }  

}

接受页面参数的javabean
package com.st2.test.mode;
public class Student
{
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
jsp:
  <body>
  <s:fielderror fieldName="deleteError"  theme="simple"/>
    <h1 style="background-color: red">删除失败!</h1>
    <s:property value="errors.deleteError[1]"/>
    <s:debug></s:debug>
    获取作用域对象的应用 request,session,application,HTTPServletRequest,HTTPServletResponse,ServletContext的引用:<br/>
    <form action="/Student_delete" method="post">
    姓名:<input type="text" name="user.name"/>
    年龄:<input type="text" name="user.age">
    </form>
    <br/>
    <input type="submit" value="提交"/><br/>
    <%=request.getAttribute("hwq")%><br/>
    ${hwq}
  </body>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值