struts2直接返回字符串

调用Action后,Action不返回任何页面,只返回一串字符串

方法一:

Action.java中的代码如下。struts.xml中配置不变

public String execute() throws Exception {  
//注意:加上这句就必须设置响应的编码格式,否则会出现乱码  
        HttpServletResponse response = ServletActionContext.getResponse();  
        response.setContentType("text/html;charset=UTF-8");  
        PrintWriter out = response.getWriter();  
  
        String dbusername = "Charles";  
        if (username.equals(dbusername)) {  
             out.println("seccess");//返回的字符串数据  
            return null;  
        }  
        return null;  
    } 

方法二:

javaAction代码:

public class TextStringAction extends ActionSupport{  
    // input属性  
    private String username;  
    private String password;  
    // output属性  
    private InputStream inputStream; //这个名字和struts.xml中对应,不能写错  
  
    public InputStream getInputStream() {  
        return inputStream;  
    }  
  
    public void setInputStream(InputStream inputStream) {  
        this.inputStream = inputStream;  
    }  
  
    public String execute() throws Exception {  
        String dbusername = "Charles";  
        if (username.equals(dbusername)) {  
            inputStream = new ByteArrayInputStream("我\\是\\好人"  
                    .getBytes("UTF-8"));  
            return "success";  
        }  
        return null;  
    }  
        // ------------省略getter/setter---------  
   } 

Struts.xml

  
          
            text/html  
            inputStream  
          
      

效果

Paste_Image.png

你可能感兴趣的:(struts2直接返回字符串)