struts2配置result

阅读更多
1. struts2配置result

新建项目HeadFirstStruts2Chap02_07

   
1) type默认是dispatcher内部转发

2) type为redirect重定向

3) type为chain链条

4) type为redirectAction重定向到action

5) result全局配置

HelloAction.java

package com.andrew.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
    private String name;
    private String error;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getError() {
        return error;
    }
    public void setError(String error) {
        this.error = error;
    }
    @Override
    public String execute() throws Exception {
        if (name == null || "".equals(name)) {
            this.error = "name是空";
            return ERROR;
        }
        return SUCCESS;
    }
    public String redirectMethod() throws Exception{
        return "redirectMethodResult";
    }
    public String chainMethod() throws Exception{
        return "chainMethodResult";
    }
    public String redirectActionMethod() throws Exception{
        return "redirectActionMethodResult";
    }
}

HelloAction2.java

package com.andrew.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction2 extends ActionSupport {
    private String name2;
    public String getName2() {
        return name2;
    }
    public void setName2(String name2) {
        this.name2 = name2;
    }
    @Override
    public String execute() throws Exception {
        this.name2 = "你好啊";
        return SUCCESS;
    }
}

struts.xml


    
    
        
            error.jsp
        
        
            success.jsp
            success.jsp
            hello2
            hello2
        
        
            success.jsp
        
    


index.jsp

默认转发_dispatcher
重定向_redirect
链条_chain
重定向到Action_redirectAction
全局result配置
success.jsp Name:${name }
Name2:${name2 } error.jsp 错误信息:${error } 运行结果: http://localhost:8080/HeadFirstStruts2Chap02_07/index.jsp 默认转发_dispatcher http://localhost:8080/HeadFirstStruts2Chap02_07/hello?name=Struts2 Name:Struts2 Name2: 重定向_redirect http://localhost:8080/HeadFirstStruts2Chap02_07/success.jsp Name: Name2: 链条_chain http://localhost:8080/HeadFirstStruts2Chap02_07/hello!chainMethod?name=Struts2 Name:Struts2 Name2:你好啊 重定向到Action_redirectAction http://localhost:8080/HeadFirstStruts2Chap02_07/hello2.action Name: Name2:你好啊 全局result配置 http://localhost:8080/HeadFirstStruts2Chap02_07/hello 错误信息:name是空


6) freemarkerfreemarker模版

7) httpheader返回一个已配置好的HTTP头信息响应

8) stream将原始数据作为流传递回浏览器端

9) velocity呈现Velocity模板

10) xslt该XML可以通过XSL模板进行转换

11) plaintext返回普通文本类容

你可能感兴趣的:(Java,struts2)