Struts2第五天

第八节:  result配置

1.     type默认是dispatcher内部转发

2.     type为redirect重定向

3.     type为chain链条

4.     type为redirectAction重定向到action

上面4个常用,一定要掌握


其他     freemarker     freemarker模版

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

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

            velocity           呈现Velocity

            xlst                 该XML可以通过XSL模板进行转换

            plaintext          返回普通文本内容


result全局配置


Action


HelloAction

package com.java1234.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{

     /**
     *
     */
     private static final long serialVersionUID = 1L;
    
     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 r()throws Exception{
          return "r";
     }
    

     public String c()throws Exception{
          return "c";
     }


     public String ra()throws Exception{
          return "ra";
     }

   
}

HelloAction2


package com.java1234.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction2 extends ActionSupport{

     /** 
     *
     */
     private static final long serialVersionUID = 1L;
    
     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;
     }     
}

XML


<?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.enable.DynamicMethodInvocation" value="true" />
 
  <package name="manage" namespace="/" extends="struts-default">
       <global-results>
            <result name="error">error.jsp</result>
       </global-results>

      
       <action name="hello" class="com.java1234.action.HelloAction">
            <result name="success" type="dispatcher">success.jsp</result>
            <result name="r" type="redirect">success.jsp</result>
            <result name="c" type="chain">hello2</result>
            <result name="ra" type="redirectAction">hello2</result>
       </action>
      
       <action name="hello2" class="com.java1234.action.HelloAction2">
            <result name="success" type="dispatcher">success.jsp</result>
       </action>
  </package>

</struts>

JSP

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="hello?name=Struts2" target="_blank">默认转发_dispatcher</a><br/>
<a href="hello!r?name=Struts2" target="_blank">重定向_redirect</a><br/>
<a href="hello!c?name=Struts2" target="_blank">链条_chain</a><br/>
<a href="hello!ra?name=Struts2" target="_blank">重定向到Action_redirectAction</a><br/>

<a href="hello" target="_blank">全局result配置</a><br/>
</body>
</html>

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
错误信息:${error }
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Name:${name }<br/>
Name2:${name2 }
</body>
</html>


你可能感兴趣的:(Struts2第五天)