美文网首页struts
struts2直接返回字符串

struts2直接返回字符串

作者: 思而忧 | 来源:发表于2017-03-24 14:07 被阅读0次

调用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

<action name="testString" class="com.xxx.xxx.xxxx" method="xxx">  
        <result type="stream">  
            <param name="contentType">text/html</param>  
            <param name="inputName">inputStream</param>  
        </result>  
    </action>  

效果

Paste_Image.png

相关文章

  • struts2直接返回字符串

    调用Action后,Action不返回任何页面,只返回一串字符串 方法一: Action.java中的代码如下。s...

  • Struts2返回json数据

    Struts2可以使用Servlet的API返回拼接的json字符串,但是这种方式既麻烦又要依赖Servlet,所...

  • String字符串操作

    字符串长度 length 属性返回字符串的长度 字符串拼接 直接用+号:String a = "I"; Strin...

  • SpringBoot跳转jsp页面问题

    SpringBoot项目,直接通过在Controller(Action)通过返回字符串直接跳转jsp页面会出现异常...

  • NDK开发错误 use of invalid jobject 0

    JNI中直接返回C/C++的字符串时会报如下错误 需要将C/C++中的字符串转换为中间层jstring返回 Ndk...

  • 理解String.intern()

    java.lang.String intern方法用来返回常量池中的某字符串,如果常量池中已经存在该字符串,则直接...

  • JavaScript String方法

    废话不多说,直接看代码 String.startsWith() 返回布尔值,表示参数字符串是否在原字符串的头部。 ...

  • 深入理解Java String.intern()

    大家可能都知道String.intern()的作用,调用它时,如果常量池中存在当前字符串, 就会直接返回当前字符串...

  • String

    5.22字符串学习 字符串对象 字符串:由多个字符组成的只读数组只读:所有API都不能直接修改原字符串,必须返回新...

  • 将/替换为-

    亲测:replace方法会返回替换后的字符串,但不会改变原字符串,若有需要可直接赋值。测试过程如下:

网友评论

    本文标题:struts2直接返回字符串

    本文链接:https://www.haomeiwen.com/subject/kbcfottx.html