我在 netbeans webapp 项目中使用 struts2。思路如下:
我制作了一个 Web 应用程序,您可以在其中将文本放入 textarea,然后单击一个按钮,然后在数据库中比较每个单词。匹配的单词出现在文本区域下方的 iframe 中。
这个过程现在使用 Servlets 完成并且工作正常,但是......我如何在 struts2 中做到这一点?
我有下一个代码:
struts.xml
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="NLPServStruts" class="com.uem.gsi.tmt.action.NLPServStrutsAction">
<result>/index.jsp</result>
</action>
</package>
</struts>
索引.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-15"
pageEncoding="ISO-8859-15"%>
<%@page import="java.io.*"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tourist Face Web Tool: Tourist Text</title>
<!--<link type="text/css" href="themes/smoothness/jquery-ui-1.8.2.custom.css" rel="stylesheet" />-->
<link rel="stylesheet" type="text/css" href="themes/base/ui.all.css">
</head>
<body>
<form method=POST id="submit" action="/TouristFaceStruts/NLPServStruts" target=output>
<h1>Tourist Face Tool (TMT)</h1>
<table>
<tr>
<td width="50%">
<button type=submit id="bSearch" style="font-size: 1.2em;"
class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">
PROCESS TEXT</button>
</td>
<td>
<SELECT NAME="selCombo" SIZE=1 onchange="descargaVideo(value);cargaTexto(value);">
<%= line %>
</SELECT>
</td>
</tr>
<tr>
<td valign="top">
<textarea name="text" id="txTourism" rows="10"
cols="72"></textarea>
<p>
<iframe name="output" id="output"
height="200" width="600">
</iframe>
</td>
<td>
<div id="myDiv" name="myDiv">
<OBJECT name="movie5" id="movie5" CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab" WIDTH="560" HEIGHT="350" >
<PARAM NAME="autoplay" VALUE="true" >
</OBJECT>
</div>
</div>
</td>
</tr>
</table>
</div>
<!--<input type=submit value=Submit>--></form>
<!--<div name="output" id="output"></div>-->
<div id="loading"
class="ui-dialog ui-widget ui-widget-content ui-corner-all">
<p>Loading...</p>
</div>
</body>
</html>
Servlet.java
package com.uem.gsi.tmt.serv;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.uem.gsi.tmt.nlp.GateGazTour;
/*RAFA
* import com.uem.gsi.tmt.nlp.NCBOAnnot;
RAFA*/
/**
* Servlet implementation class NLPServ
*/
public class NLPServ extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public NLPServ() {
super();
System.out.println("constructor");
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/* RAFA
* String lev=(request.getParameter("lev")!=null)?request.getParameter("lev"):"0";
String onts=(request.getParameter("onts")!=null)?request.getParameter("onts"):"40397"; //Medlineplus
RAFA */
String text=(request.getParameter("text")!=null)?request.getParameter("text"):"";
String nlpResults="";
//PrintWriter out = response.getWriter();
if (text.equals("")){
//out.println("Text to annotate can't be null");
nlpResults="Text to annotate can't be null";
}else{
try {
//GATE Annotation
//out.println(htmlGateTree(text));
nlpResults=htmlGateTree(text);
//NCBO Annotation
/*NCBOAnnotCall oNCBO=new NCBOAnnotCall();
oNCBO.setText(text);
oNCBO.setOntInRes(onts);
oNCBO.setlevelMax(lev);
//out.println(oNCBO.doRESTQuery());
//out.println(htmlNCBOTree(text,oNCBO.doRESTQuery()));
nlpResults+=htmlNCBOTree(text,oNCBO.doRESTQuery());*/
/*RAFA
* NCBOAnnot ncboAnnot=new NCBOAnnot();
RAFA*/
//System.out.println("level: "+lev);
/*RAFA
ncboAnnot.runNCBOAnnot(text, onts, lev);
nlpResults+=htmlNCBOTree(ncboAnnot);
RAFA*/
}
catch( Exception e ) {
//out.println(e.toString());
nlpResults=e.toString();
}
}
request.setAttribute("nlpResults", nlpResults);
getServletConfig().getServletContext().
getRequestDispatcher("/nlpresult.jsp").forward(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
private String htmlGateTree(String text) throws Exception {
String result="";
String sc = getServletContext().getRealPath("/");
GateGazTour gateAnnot=new GateGazTour(sc);
gateAnnot.runGazetteer(text);
//Add value just if is not repeated
List<String> lTouristT=gateAnnot.getTouristAttraction(),lTouristAttraction=new ArrayList<String>();
List<String> lAccommodationT=gateAnnot.getAccommodation(),lAccommodation=new ArrayList<String>();
List<String> lTravelDestinationT=gateAnnot.getTravelDestination(),lTravelDestination=new ArrayList<String>();
//List<String> lFromT=gateAnnot.getFrom(),lFrom=new ArrayList<String>();
//List<String> lToT=gateAnnot.getTo(),lTo=new ArrayList<String>();
int sizeT=lTouristT.size();
for (int i=0;i<sizeT;i++){
if (!lTouristAttraction.contains(lTouristT.get(i))){
lTouristAttraction.add(lTouristT.get(i));
//lFrom.add(lFromT.get(i));
//lTo.add(lToT.get(i));
}
}
sizeT=lAccommodationT.size();
for (int i=0;i<sizeT;i++)
if (!lAccommodation.contains(lAccommodationT.get(i)))
lAccommodation.add(lAccommodationT.get(i));
sizeT=lTravelDestinationT.size();
for (int i=0;i<sizeT;i++)
if (!lTravelDestination.contains(lTravelDestinationT.get(i)))
lTravelDestination.add(lTravelDestinationT.get(i));
//
Iterator<String> iter = lTouristAttraction.iterator();
while (iter.hasNext()){
String annotName=iter.next();
result+="<tr>";
result+="<td><a href='fbresulttou.jsp?term="+annotName+"' target=_blank>"+annotName+"</a></td>";
result+="<td>Freebase Tourist Attraction</td>";
//result+="<td></td>";
//result+="<td>Freebase Disease</td>";
//result+="<td>Gazetteer</td>";
result+="</tr>";
}
iter = lAccommodation.iterator();
while (iter.hasNext()){
String annotName=iter.next();
result+="<tr>";
result+="<td><a href='fbresultaco.jsp?term="+annotName+"' target=_blank>"+annotName+"</a></td>";
result+="<td>Freebase Accommodation</td>";
//result+="<td></td>";
//result+="<td>Freebase Symptom</td>";
//result+="<td>Gazetteer</td>";
result+="</tr>";
}
iter = lTravelDestination.iterator();
while (iter.hasNext()){
String annotName=iter.next();
result+="<tr>";
result+="<td><a href='fbresulttra.jsp?term="+annotName+"' target=_blank>"+annotName+"</a></td>";
result+="<td>Freebase Travel Destination</td>";
//result+="<td></td>";
//result+="<td>Freebase Treatment</td>";
//result+="<td>Gazetteer</td>";
result+="</tr>";
}
return result;
}
/*RAFA
private String htmlNCBOTree(NCBOAnnot oNCBOAnnot) throws Exception {
String result="";
//Add value just if is not repeated
List<String> lConceptT=oNCBOAnnot.getConcept(),lConcept=new ArrayList<String>();
List<String> lLocalOntologyIdT=oNCBOAnnot.getLocalOntologyId(),lLocalOntologyId=new ArrayList<String>();
List<String> lFullIdT=oNCBOAnnot.getFullId(),lFullId=new ArrayList<String>();
List<String> lIsDirectT=oNCBOAnnot.getIsDirect(),lIsDirect=new ArrayList<String>();
List<String> lFromT=oNCBOAnnot.getFrom(),lFrom=new ArrayList<String>();
List<String> lToT=oNCBOAnnot.getTo(),lTo=new ArrayList<String>();
List<String> lPreferredNameT=oNCBOAnnot.getPreferredName(),lPreferredName=new ArrayList<String>();
List<String> lContextNameT=oNCBOAnnot.getContextName(),lContextName=new ArrayList<String>();
int sizeT=lConceptT.size();
for (int i=0;i<sizeT;i++){
if (!lConcept.contains(lConceptT.get(i))){
lConcept.add(lConceptT.get(i));
lLocalOntologyId.add(lLocalOntologyIdT.get(i));
lFullId.add(lFullIdT.get(i));
lIsDirect.add(lIsDirectT.get(i));
lPreferredName.add(lPreferredNameT.get(i));
lContextName.add(lContextNameT.get(i));
}
}
//
int size=lConcept.size();
//Paint results
for (int i=0;i<size;i++){
if (lLocalOntologyId.get(i).equals("40397")){
result+="<tr><td><a href='/MedicalFace/mlpresult.jsp?term="+
lConcept.get(i)+"' target=_blank>"+lConcept.get(i)+
"</a></td><td>MedlinePlus</td></tr>";
//lFullId.get(i)+"</td><td>"+
//lPreferredName.get(i);
//"</td><td>"+lContextName.get(i)+"</td></tr>";
}else{
result+="<tr><td>"+lConcept.get(i)+"</td><td>"+lLocalOntologyId.get(i)+
"</td></td></tr>";
//lFullId.get(i)+"</td><td>"+
//lPreferredName.get(i)+"</td></tr>";
//"</td><td>"+lContextName.get(i)+"</td></tr>";
}
}
return result;
/* Document doc = MyDOMParserBean.getDocument(xml);
NodeList listAnnot=doc.getElementsByTagName("annotationBean");
String result="";
for (int i=0;i<listAnnot.getLength();i++){
result+="<tr>";
//Initialize data to show
String localOntologyId="",fullId="",preferredName="";
String contextName="",isDirect="",from="",to="",nameInText="";
Node annotNode=listAnnot.item(i);
//System.out.println(listAnnot.item(i).getNodeName());
NodeList listChildAnnot=annotNode.getChildNodes();
for (int j=0;j<listChildAnnot.getLength();j++){
Node nodeTemp1=listChildAnnot.item(j);
//System.out.println(nodeTemp1.getNodeName());
//Concept
if (nodeTemp1.getNodeName().equals("concept")){
//System.out.println(nodeTemp1.getNodeName());
NodeList listChildRes=nodeTemp1.getChildNodes();
for (int k=0;k<listChildRes.getLength();k++){
Node node1=listChildRes.item(k);
String node1name=node1.getNodeName();
//Elements selection
if (node1name.equals("localOntologyId"))
localOntologyId=node1.getFirstChild().getNodeValue().trim();
else if (node1name.equals("fullId"))
fullId=node1.getFirstChild().getNodeValue().trim();
else if (node1name.equals("preferredName"))
preferredName=node1.getFirstChild().getNodeValue().trim();
}
}//Context
else if (nodeTemp1.getNodeName().equals("context")){
//System.out.println(nodeTemp1.getNodeName());
NodeList listChildRes=nodeTemp1.getChildNodes();
for (int k=0;k<listChildRes.getLength();k++){
Node node1=listChildRes.item(k);
String node1name=node1.getNodeName();
//System.out.println(node1.getNodeName()+"="+node1.getNodeValue());
//Seleccionar elementos
if (node1name.equals("contextName"))
contextName=node1.getFirstChild().getNodeValue().trim();
else if (node1name.equals("isDirect"))
isDirect=node1.getFirstChild().getNodeValue().trim();
else if (node1name.equals("from"))
from=node1.getFirstChild().getNodeValue().trim();
else if (node1name.equals("to"))
to=node1.getFirstChild().getNodeValue().trim();
}//else if (node1name.equals("term"))
}
}
nameInText=text.substring(new Integer(from)-1,new Integer(to));
//System.out.println(localOntologyId+","+fullId+","+preferredName+","+
// contextName+","+isDirect+","+from+","+to+","+nameInText);
//if (localOntologyId.equals("40397"))
nameInText="<a href='"+fullId+"'>"+nameInText+"</a>";
result+="<td>"+nameInText+"</td><td>"+localOntologyId+"</td><td>"+
fullId+"</td><td>"+preferredName+"</td><td>"+contextName+"</td>";
result+="</tr>";
}
return result;*/
/*} RAFA*/
/*Jorge de Lemos Martín*/
protected void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
所以,问题是:当我点击按钮时,在 iframe 中加载同一个页面 index.jsp 我做错了什么?
太感谢了!