我是个新手 请教下form提交上去怎么不显示结果,各位大侠帮我看看哪错了
一,struts.xml<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="user" extends="struts-default" namespace="/user">
<action name="user" class="com.zhurong.action.ErrorAction">
<result name="error">/Error.jsp</result>
<result>/Success.jsp</result>
</action>
</package>
</struts>
二,index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="user">
请输入姓名:<br>
<input type="text" name="name"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
三,Error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Error.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
User add error!
<s:property value="error.name[0]"/><br>
<s:debug></s:debug>
</body>
</html>
四,Success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
Success<br>
</body>
</html>
六,action
package com.zhurong.action;
import com.opensymphony.xwork2.ActionSupport;
public class ErrorAction extends ActionSupport {
private String name;
public String getName (){
return name;
}
public void setName (String name){
this.name = name;
}
public String add() {
if(name == null || !name.equals("admin")){
this.addFieldError("name","name is error");
return "error";
}else {
return "success";
}
}
}
程序的大体意思是先调出index.jsp在里面输入姓名,如果是admin页面跳转到Success.jsp,
其他情况跳转到error.jsp
可是我输入姓名后点提交时就出错 为什么啊。。。。。。谢谢 --------------------编程问答-------------------- <form action="user">
请输入姓名:<br>
<input type="text" name="name"/>
<input type="submit" value="提交"/>
</form>
改用struts2表单
<s:form> --------------------编程问答-------------------- 你的路径有误,namespace设置了user,action名为user,都得加上 --------------------编程问答-------------------- 具体怎么写啊 谢谢啊 --------------------编程问答-------------------- <form action="user" namespace="/user">试试 --------------------编程问答-------------------- 你的配置没写应该调用action的哪个方法,默认调用execute()方法。你里面的add方法根本就不走
--------------------编程问答-------------------- <form action="user/user!add.action">
写成这样也不行啊 --------------------编程问答-------------------- 大侠们麻烦在帮我看看 --------------------编程问答-------------------- method="add",如果还不行,把错误贴出来
--------------------编程问答-------------------- 刚刚我把myeclipse重启了下,现在又可以了 --------------------编程问答-------------------- 你的路径有误,namespace设置了user,action名为user,都得加上
--------------------编程问答--------------------
顶一个 --------------------编程问答-------------------- 好吧 我帮不上什么忙了 这个我们是新开的 刚入门 应该连入门还不算吧 --------------------编程问答-------------------- <result>/Success.jsp</result> 加上 name='success'
还有你Action:ErrorAction这个类,执行的是execute方法,你没的,看看吧 --------------------编程问答-------------------- 在配置文件中制定你要执行的add方法,要不然的话程序会执行execute方法的
在<action>标签中加入method="add"
补充:Java , Web 开发