ShellAction.java
package com.xx.xx.spider.action;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import javax.script.*;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ShellAction extends BaseAction implements ApplicationContextAware {
private static final Logger logger = LoggerFactory.getLogger(ShellAction.class);
private String input="";
private String output="";
private String engineName="";
private List<String> engineNames;
private ApplicationContext ac;
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
logger.debug("engineName {}",this.getEngineName());
// this.setEngineName(this.getEngineName());
ScriptEngine currentEngine=null;
if(StringUtils.isNotBlank(this.getEngineName()))
{
currentEngine=new ScriptEngineManager().getEngineByName(this.getEngineName());
Bindings bindings=currentEngine.createBindings();
bindings.put("__ac", this.ac);
bindings.put("__request", ServletActionContext.getRequest());
bindings.put("__response", ServletActionContext.getResponse());
bindings.put("__sc", ServletActionContext.getServletContext());
bindings.put("__context", ServletActionContext.getContext());
StringWriter sw = new StringWriter();
// PrintWriter pw = new PrintWriter(sw);
// currentEngine.getContext().setWriter(pw);
currentEngine.getContext().setWriter(sw);
boolean success=true;
try{
currentEngine.eval(this.getInput(),bindings);
}
catch(Exception ex)
{
success=false;
this.setOutput(sw.getBuffer().toString()+"\n"+ex.toString());
}
if (success)
{
this.setOutput(sw.getBuffer().toString());
}
}
return SUCCESS;
}
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
public String getOutput() {
return output;
}
public void setOutput(String output) {
this.output = output;
}
public List<String> getEngineNames() {
return engineNames;
}
public void setEngineNames(List<String> engineNames) {
this.engineNames = engineNames;
}
public String getEngineName() {
return engineName;
}
public void setEngineName(String engineName) {
this.engineName = engineName;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
// TODO Auto-generated method stub
this.ac=applicationContext;
}
}
applicaitonContext-action.xml
<bean id="shellAction" class="com.xx.xx.spider.action.ShellAction">
<property name="engineNames">
<list>
<value>python</value>
<value>ruby</value>
<value>groovy</value>
</list>
</property>
</bean>
structs.xml
<action name="shell" class="shellAction">
<result type="dispatcher" name="success">
<param name="location">/WEB-INF/jsp/shell.jsp</param>
</result>
</action>
shell.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%--
Created by IntelliJ IDEA.
User: mlzboy
Date: 12-2-19
Time: 下午1:24
To change this template use File | Settings | File Templates.
--%>
<html>
<head>
<title>shell</title>
<script src="${pageContext.request.contextPath}/js/jquery-1.4.2.min.js"></script>
</head>
<body>
<form action="${pageContext.request.contextPath}/shell.do" name="form1" method="post">
<p>
<select id="engineName" name="engineName">
<c:forEach var="item" items="${engineNames}" varStatus="status">
<option value="${item}">${item}</option>
</c:forEach>
</select>
<input type="submit" name="sub" value="提交" />
</p>
<textarea id="input" name="input" rows="10" cols="80">${input}</textarea>
</form>
<textarea name="output" rows="10" cols="80">${output}</textarea>
</body>
</html>
<script>
$("#input").focus();
$('#engineName option[value="${engineName}"]').attr("selected", "selected");
</script>