会struts2的高手来帮帮忙

yjm5158 2009-03-26 11:26:19
首先描述一下我的系统的功能吧,我做的是一个在线考试系统。考试的题目是从后台mainframe的cobol程序选题,然后我通过MQ接受题目消息,然后在前台页面显示考题,并且计算分数。我用到了struts2的框架,在配置action的时候出现了error,一时找不到原因,所以请各位大虾帮忙。下面贴出我的代码和错误提示。

web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
examinationSystem</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>StartExam.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<!--配置Struts 2的核心实现类-->
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<!--拦截所有的URL请求-->
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

struts.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>

<package name="name" extends="struts-default">
<action name="Exam" class="ESAction.examAction">
<result>/exam.jsp</result>
</action>

<action name="submit" class="ESAction.submitAction">
<result>/score.jsp</result>
</action>
</package>
</struts>
action都放在ESAction这个包里面

错误提示:

[3/26/09 11:25:01:421 CST] 00000024 WebApp E [Servlet Error]-[Unable to instantiate Action, ESAction.examAction, defined for 'Exam' in namespace '/'null]: com.ibm.ws.webcontainer.webapp.WebAppErrorReport: Unable to instantiate Action, ESAction.examAction, defined for 'Exam' in namespace '/'null
at com.ibm.ws.webcontainer.webapp.WebAppDispatcherContext.sendError(WebAppDispatcherContext.java:536)
at com.ibm.ws.webcontainer.srt.SRTServletResponse.sendError(SRTServletResponse.java:930)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:725)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:130)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:87)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:701)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:646)
at com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.invokeFilters(DefaultExtensionProcessor.java:628)
at com.ibm.ws.wswebcontainer.extension.DefaultExtensionProcessor.invokeFilters(DefaultExtensionProcessor.java:145)
at com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.handleRequest(DefaultExtensionProcessor.java:595)
at com.ibm.ws.wswebcontainer.extension.DefaultExtensionProcessor.handleRequest(DefaultExtensionProcessor.java:111)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3129)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:238)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:811)
at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1433)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:93)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:465)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:394)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:274)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:152)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:213)
at com.ibm.io.async.AbstractAsyncFuture.fireCompletionActions(AbstractAsyncFuture.java:195)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:194)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:741)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:863)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1510)

[3/26/09 11:25:01:468 CST] 00000024 SRTServletRes W WARNING: Cannot set status. Response already committed.
[3/26/09 11:25:01:468 CST] 00000024 SRTServletRes W WARNING: Cannot set header. Response already committed.


...全文
678 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
tyouvivi 2009-03-27
  • 打赏
  • 举报
回复
<form action="submit.action" method="post">
改成

<s:form action="submit.action" method="post">
要用struts的标签
yjm5158 2009-03-26
  • 打赏
  • 举报
回复
up up help help
yjm5158 2009-03-26
  • 打赏
  • 举报
回复
不好意思,第一次用struts架构,namespace属性里面应该加点什么啊?不太明白这个属性的意思~我加了"/"也是出错
heting1024 2009-03-26
  • 打赏
  • 举报
回复
你没看见三楼的红色部分吗?你的配置文件没加namespace=""加上就好了
yjm5158 2009-03-26
  • 打赏
  • 举报
回复
晕,你误会了~四楼 快帮忙。
yjm5158 2009-03-26
  • 打赏
  • 举报
回复
Attribute "namespace" must be declared for element type "action".
这么改也不对啊~
heting1024 2009-03-26
  • 打赏
  • 举报
回复
楼主北大青鸟的
heting1024 2009-03-26
  • 打赏
  • 举报
回复
<package name="name" extends="struts-default" namespace="">
yjm5158 2009-03-26
  • 打赏
  • 举报
回复
exam.jsp :
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>分页显示Question </title>
</head>
<body onload="showTime()">
<div align="center">
<script>
//时间倒计时脚本
function showTime(){
var timeLeft=10; //剩余时间
var i=0; //计数器,计算setTimeout需要的延迟时间
updateTime(timeLeft); //先更新一下时间表
while(timeLeft>0){
timeLeft--;
i++;
//每一分钟1000*60*i时间减一,同时更新显示时间
setTimeout("updateTime("+timeLeft+")", 1000*60*i);
}
//循环完毕提交表单
setTimeout("document.form1.submit()", 1000*60*i);
}
function updateTime(timeLeft){
document.getElementById("span").innerHTML="距考试结束还剩"+timeLeft+"分钟";
}

function saveAnswers() {
var questionCount=5;
var i;
for(i=0;i<questionCount;i++){
var value=getRadioValue(i);
document.cookie=i+"="+value;
}
return true;
}
//取得radio被选中的值
function getRadioValue(radioName)
{
var obj = document.getElementsByName("question"+radioName);
for(i=0;i<obj.length;i++){
if(obj[i].checked){
return obj[i].value;
}
}
return "f";
}
</script>

<form action="submit.action" method="post">
<s:iterator value="question" status="stat">

<table border="1">
<tr>
<!--#stat.index+1动态的输出题号-->
<td width="800" height="56"><s:property value="#stat.index+1"/>.<s:property value="questionContent"/></td>
</tr>
<tr>
<td height="54" align="left">
<!--迭代输出所有考题-->
<!--因为每组radio都需要有不同而且有顺序的名字,所有用question加上题的顺序数做名字-->
<s:radio list="answers" name="%{'question'+#stat.index}" theme="simple" value="yourAnswer"/>
</td>
</tr>
</table>

</s:iterator>

<table border="1">
<tr>

</tr>
</table>

<s:url id="url_pre" value="Exam.action">
<s:param name="pageNow" value="pageNow-1"> </s:param>
</s:url>

<s:url id="url_next" value="Exam.action">
<s:param name="pageNow" value="pageNow+1"> </s:param>
</s:url>

<s:if test="pageNow==1">
上一页
</s:if>
<s:else>
<s:a href="%{url_pre}" onclick="return saveAnswers()">上一页 </s:a>
</s:else>
<s:if test="pageNow>=totalPage">
下一页
</s:if>
<s:else>
<s:a href="%{url_next}" onclick="return saveAnswers()">下一页 </s:a>
</s:else>
<input type="submit" value="交 卷">
</form>
</div>
</body>
</html>


拜托各位大虾了,实在找不到问题所在了
yjm5158 2009-03-26
  • 打赏
  • 举报
回复
下面是examAction的代码:
package ESAction;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import Dao.questionDAO;
import ESBean.Question;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class examAction extends ActionSupport implements ServletRequestAware{
private List <Question> question;

private int totalPage ;
private int pageNow = 1 ; // current page
private int pageSize = 5 ; // one page show 5 question;
private questionDAO questionDAO = new questionDAO() ;
private List <Question> list = new ArrayList <Question> () ; //List for all question

private HttpServletRequest servletRequest;


//get corrent page answers
public void getAnswer(int pageSize, int pageNow){
Cookie[] cookies = servletRequest.getCookies();
for(int i = 0; i < pageSize; i++){
list.get((pageNow - 1) * pageSize + i).setYourAnswer(cookies[i].getValue());
}
questionDAO.setList(list);
}

// get corrent page question list
public List <Question> queryByPage (int pageSize, int pageNow) {
List <Question> partList = new ArrayList <Question> () ; //List for a part of question
list = questionDAO.getList();
if(list.size() % pageSize == 0){
totalPage = list.size()/pageSize;
}else{
totalPage = list.size()/pageSize + 1 ;
}
for(int i = 0; i < pageSize; i++ ){
//read question for corrent page
partList.add(list.get((pageNow - 1) * pageSize + i));
}
return partList ;
}

public String execute() throws Exception {
ActionContext ctx = ActionContext.getContext();
question = queryByPage(pageSize, pageNow) ;
getAnswer(pageSize, pageNow);
//put list which have answers into session
ctx.getSession().put("list", list);

//put page info into session
ctx.getSession().put("pageSize", pageSize);
ctx.getSession().put("pageNow", pageNow);
return "success" ;
}

public int getPageNow() {
return pageNow;
}

public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

/* public questionDAO getQuestionDAO() {
return questionDAO;
}

public void setQuestionDAO(questionDAO questionDAO) {
this.questionDAO = questionDAO;
}*/

public List<Question> getQuestion() {
return question;
}

public void setQuestion(List<Question> question) {
this.question = question;
}

public int getTotalPage() {
return totalPage;
}

public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public HttpServletRequest getServletRequest() {
return servletRequest;
}

public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
}
}
questionDAO.java :

package Dao;

import java.util.ArrayList;
import java.util.List;

import ESBean.Question;
import ESMQ.MessageQueue;

public class questionDAO {

private Question question ;
public List <Question> list ;

//read all questions
public questionDAO () {
List <Question> list = new ArrayList <Question> () ;
for(int i = 0; i < 20; i++){
question.setQuestionContent("this is question" + (i + 1)) ;
question.setId("0000" + i) ;
question.setAnswerA("this is answer a") ;
question.setAnswerB("this is answer b") ;
question.setAnswerC("this is answer c") ;
question.setAnswerD("this is answer d") ;
question.setAnswerE("this is answer e") ;
question.setCategory("Y") ;
question.getAnswers() ;
list.add (question) ;
}

/*
List <Question> list = new ArrayList <Question> () ;
MessageQueue MQ = new MessageQueue() ;
StringBuffer preMessage = new StringBuffer(1600) ;
preMessage.append(MQ.readQ()) ;
for (int i = 0; i < Integer.parseInt(preMessage.substring(1579, 1581)); i++){
//new a StringBuffer which size is 1600
StringBuffer message = new StringBuffer(1600) ;
//put message into StringBuffer
message.append(MQ.readQ()) ;
//get MessageFlag to judge wether the message is legal
String MessageFlag = message.substring(9, 9) ;
if("Y".equals(MessageFlag)){
question.setQuestionContent(message.substring(48, 302)) ;
question.setId(message.substring(10, 21)) ;
question.setAnswerA(message.substring(303, 557)) ;
question.setAnswerB(message.substring(558, 812)) ;
question.setAnswerC(message.substring(813, 1067)) ;
question.setAnswerD(message.substring(1068, 1322)) ;
question.setAnswerE(message.substring(1323, 1577)) ;
question.setCategory(message.substring(1578, 1578)) ;
question.getAnswers() ;
list.add (question) ;
}else{
i-- ;
continue;
}
}
*/}

public List<Question> getList() {
return list;
}

public void setList(List<Question> list) {
this.list = list;
}
}

yjm5158 2009-03-26
  • 打赏
  • 举报
回复
我改成action="/ESAction/submit.action"
<package name="mypackage2" extends="struts-default" namespace="/ESAction">
<action name="Exam" class="ESAction.examAction">
<result name="success">exam.jsp</result>
</action>
错误是There is no result type
我加上了result type=“dispatcher”
错误是There is no result type defined for type 'dispatcher' mapped with name 'success'.
在package里面加上extends=“struts-defaults”
错误是:There is no Action mapped for namespace / and action name Exam

我真是服了啊~
qq278095755 2009-03-26
  • 打赏
  • 举报
回复
namespace 的原因。namespace="test"
/项目名/test/action方法名.do
donnydream 2009-03-26
  • 打赏
  • 举报
回复
http://struts.apache.org/2.1.6/docs/package-configuration.html
上面写的很清楚
The namespace attribute subdivides action configurations into logical modules, each with its own identifying prefix. Namespaces avoid conflicts between action names. Each namespace can have its own "menu" or "help" action, each with its own implementation. While the prefix appears in the browser URI, the tags are "namespace aware", so the namespace prefix does not need to be embedded in forms and links.

Default Namespace

The default namespace is "" - an empty string. The default namespace is used as a "catch-all" namespace. If an action configuration is not found in a specified namespace, the default namespace is also be searched. The local/global strategy allows an application to have global action configurations outside of the action element "extends" hierarchy.

The namespace prefix can be registered with Java declarative security, to ensure only authorized users can access the actions in a given namespace.

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧