struts2 老是向 name="input" 的result跳转

小灰狼 2010-09-20 07:32:08
怎么设置都没用,jsp 页面提交之后,它总是去找一个 <result name="input"> 进行跳转

我在 action 类的执行方法中什么事都没做,直接返回一个自己指定的字符串 "register_ok",但是 action 还是向 name="input" 的 result 跳转。并且如果在 struts.xml 中删除 name="input" 的 result,则程序运行时报错,提示没有配置 name="input" 的节点



下面是 action 类的代码


import com.opensymphony.xwork2.*;
public class RegisterAction extends ActionSupport{
private String username;
private String password1;
private String password2;

public String doRegister() throws Exception{
return "abc";
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword1() {
return password1;
}

public void setPassword1(String password1) {
this.password1 = password1;
}

public String getPassword2() {
return password2;
}

public void setPassword2(String password2) {
this.password2 = password2;
}

}


下面是 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>

<include file="struts-default.xml" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<package name="user-manage" namespace="/usr" extends="struts-default">
<action name="register" class="com.csjl.infopub.struts2.usr.RegisterAction" method="doRegister">
<result name="input">/usr/register1.jsp</result>
<result name="success">/usr/register1.jsp</result>
</action>
</package>

<!-- Add packages here -->

</struts>


...全文
718 40 打赏 收藏 转发到动态 举报
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
我系大红花 2012-01-10
  • 打赏
  • 举报
回复
╮(╯▽╰)╭,鄙人也遇到这个问题,这个好像是和filter是有关系的,我写的代码的问题就在于我设置一个输入,类型是时间的,如果页面没有输入YYYY-MM-DD的格式,就会自动跳转到input的result。由于校验在action之前操作,所以不管你action的xml里面怎么定义,它都会直接跳到input的result里面去。学艺不精啊,浪费了我4个小时~
stonegreen72 2011-12-25
  • 打赏
  • 举报
回复
class City{
....
private Province province;
.....
}
stonegreen72 2011-12-25
  • 打赏
  • 举报
回复
同感!
好像写一个<s:hidden name="city.province"></s:hidden>
也会出现直返input的情况。
得一个一个通过hidden传入。
和你们拼了 2011-09-16
  • 打赏
  • 举报
回复
同感asdasdasd
ypb137154098 2011-02-12
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 yzsunlight 的回复:]
仔细检查你的表单提交的元素类型是否符合条件
[/Quote]
+1
jun526 2010-09-21
  • 打赏
  • 举报
回复
如果提交了还没有进指定的方法,那说明是数据验证不通过
zhy0511 2010-09-21
  • 打赏
  • 举报
回复
我觉得你应该去掉 <include file="struts-default.xml" />试试,既然你已经继承了,为什么还要引入?
zhy0511 2010-09-21
  • 打赏
  • 举报
回复
<include file="struts-default.xml" />
为什么还要引入这个?????
redlotus_lyn 2010-09-21
  • 打赏
  • 举报
回复
我的qq:75425364,帮你解决
小灰狼 2010-09-21
  • 打赏
  • 举报
回复
还有一个 json.js 文件,太大了发不上来,节取一小段


var cList =
[
{pId:110000,cId:110100,cName:"市辖区"},
{pId:110000,cId:110200,cName:"县"},
{pId:120000,cId:120100,cName:"市辖区"},
{pId:120000,cId:120200,cName:"县"}
];
var aList =
[
{cId:110100,aid:110107,aName:"石景山区"},
{cId:110100,aid:110108,aName:"海淀区"},
{cId:110100,aid:110109,aName:"门头沟区"},
{cId:110100,aid:110110,aName:"燕山区"},
{cId:110100,aid:110111,aName:"房山区"},
{cId:110100,aid:110112,aName:"通州区"},
{cId:110100,aid:110113,aName:"顺义区"},
{cId:110100,aid:110114,aName:"昌平区"},
{cId:110100,aid:110115,aName:"大兴区"},
{cId:110100,aid:110116,aName:"怀柔区"},
{cId:110100,aid:110117,aName:"平谷区"},

{cId:110200,aid:110221,aName:"昌平县"},
{cId:110200,aid:110222,aName:"顺义县"},
{cId:110200,aid:110223,aName:"通县"},
{cId:110200,aid:110224,aName:"大兴县"},
{cId:110200,aid:110225,aName:"房山县"},
{cId:110200,aid:110226,aName:"平谷县"},
{cId:110200,aid:110227,aName:"怀柔县"},
{cId:110200,aid:110228,aName:"密云县"},
{cId:110200,aid:110229,aName:"延庆县"},

{cId:120100,aid:120101,aName:"和平区"},
{cId:120100,aid:120102,aName:"河东区"},
{cId:120100,aid:120103,aName:"河西区"},
{cId:120100,aid:120104,aName:"南开区"},
{cId:120100,aid:120105,aName:"河北区"},
{cId:120100,aid:120106,aName:"红桥区"},
{cId:120100,aid:120107,aName:"塘沽区"},
{cId:120100,aid:120108,aName:"汉沽区"},
{cId:120100,aid:120109,aName:"大港区"},
{cId:120100,aid:120110,aName:"东丽区"},
{cId:120100,aid:120111,aName:"西青区"},
{cId:120100,aid:120112,aName:"津南区"},
{cId:120100,aid:120113,aName:"北辰区"},
{cId:120100,aid:120114,aName:"武清区"},
{cId:120100,aid:120115,aName:"宝坻区"},

{cId:120200,aid:120221,aName:"宁河县"},
{cId:120200,aid:120222,aName:"武清县"},
{cId:120200,aid:120223,aName:"静海县"},
{cId:120200,aid:120224,aName:"宝坻县"},
{cId:120200,aid:120225,aName:"蓟县"}
];

小灰狼 2010-09-21
  • 打赏
  • 举报
回复
完整的代码:

jsp

<%@page contentType="text/html" pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>JSP Page</title>
<script type="text/javascript" src="../js/area_json.js"></script>
<script type="text/javascript" src="../js/regular.js"></script>
<script type="text/javascript" src="../js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(
function(){
$("#txtPassword1").bind("blur",
function(){
var pwd1 = $("#txtPassword1").val();
if(pwd1.length < 6){
$("#tdPwd1").text("密码不能为空,且不能少于6个字符");
return;
}
$("#tdPwd1").text("");
var pwd2 = $("#txtPassword2").val();
if(pwd2 == ""){
$("#tdPwd2").text("请再次输入密码");
return;
}
if(pwd1 != pwd2){
$("#tdPwd2").text("两次输入的密码不相同");
return;
}
}
);

$("#txtPassword2").bind("blur",
function(){
var pwd1 = $("#txtPassword1").val();
var pwd2 = $("#txtPassword2").val();
if(pwd1 != pwd2){
$("#tdPwd2").text("两次输入的密码不相同");
return;
}
$("#tdPwd2").text("");
}
);

$("#txtEmail").bind("blur",
function(){
var email = $("#txtEmail").val();
if(email == ""){
$("#tdEmail").text("请填写您的email");
return;
}
if(!isEmail(email)){
$("#tdEmail").text("无效的email格式");
return;
}
$("#tdEmail").text("");
}
);

$("#txtUsername").bind("blur",
function(){
var username = $("#txtUsername").attr("value");
if(username.length <= 4){
$("#tdUsername").html("用户名不能小于5个字符");
return;
}
$("#tdUsername").html("");
$.ajax(
{
type:"get",
url:"./../ajax/user_exist.jsp",
data:"username=" + username,
success:function(msg){
msg = $.trim(msg);
if(msg == "exist"){
$("#tdUsername").html("用户名已经存在");
}else if(msg == "not exist"){
$("#tdUsername").html("此用户名可以使用");
}
}
}
);

}
);
}
);

function proinceChanged(){
var sP = document.getElementById("sProvince");
var sC = document.getElementById("sCity");

var provinceId = sP.value;
sC.options.length = 0;
for(var i = 0; i<cList.length; i++){
if(cList[i].pId != provinceId)
continue;
var option = document.createElement("option");
sC.appendChild(option);
option.value = cList[i].cId;
option.innerHTML = cList[i].cName;
}
cityChanged();
}
function cityChanged(){
var sC = document.getElementById("sCity");
var sA = document.getElementById("sArea");
var cId = sC.value;
sA.options.length = 0;

for(var i = 0; i<aList.length; i++){

if(aList[i].cId != cId)
continue;
var option = document.createElement("option");
sA.appendChild(option);
option.value = aList[i].aId;
option.innerHTML = aList[i].aName;
}
}
function onSubmit(){
var isOK = true;
var username = $("#txtUsername").val();
var pwd1 = $("#txtPassword1").val();
var pwd2 = $("#txtPassword2").val();
var email = $("#txtEmail").val();
var area = $("#sArea").val();

if(username.length <= 4){
$("#tdUsername").html("用户名不能少于5个字符");
$("#txtUsername").focus();
isOK = false;
}
if(pwd1.length < 6){
$("#tdPwd1").html("密码不能为空且不能少于6个字符");
$("#txtPassword1").focus();
isOK = false;
}
if(pwd2 != pwd1){
$("#tdPwd2").html("两次输入的密码不相同");
$("#txtPassword2").focus();
isOK = false;
}
if(email == ""){
$("#tdEmail").html("无效的email格式");
$("#txtEmail").focus();
isOK = false;
}else if(!isEmail(email)){
$("#tdEmail").html("无效的email格式");
$("#txtEmail").focus();
isOK = false;
}
if(area == null || area == ""){
$("#tdArea").html("请选择所在区域");
isOK = false;
}

return isOK;
}
</script>
</head>
<body>
<s:form name="registerForm" id="fForm" action="register" namespace="/usr" theme="simple" onsubmit="return onSubmit()">
<table width="800px" border="1" bordercolor="black" style="border-collapse: collapse">
<tr>
<td align="right">用户名</td>
<td><s:textfield name="username" id="txtUsername" autocomplete="off" /></td>
<td id="tdUsername">abc</td>
</tr>
<tr>
<td align="right">密码</td>
<td><s:password name="password1" id="txtPassword1" /></td>
<td id="tdPwd1"></td>
</tr>
<tr>
<td align="right">确认密码</td>
<td><s:password name="password2" id="txtPassword2" /></td>
<td id="tdPwd2"></td>
</tr>
<tr>
<td align="right">电子邮件</td>
<td><s:textfield name="email" id="txtEmail" autocomplete="off" /></td>
<td id="tdEmail"></td>
</tr>
<tr>
<td align="right">所在区域</td>
<td>
<select name="province" id="sProvince" onchange="proinceChanged()">
<s:set value="@com.csjl.infopub.db.cache.AreaCache@getInstance()" id="citySet" />
<s:iterator value="#citySet.provinces" status="st">
<option value="<s:property value='id' />"><s:property value="proName" /></option>
</s:iterator>
</select>省(直辖市)
<select name="city" id="sCity" onchange="cityChanged()"></select>市
<select name="area" id="sArea"></select>县区
</td>
<td id="tdArea"></td>
</tr>
<tr>
<td colspan="3" align="right">
<s:submit name="doRegister" title="注册" />
</td>
</tr>
</table>
</s:form>
</body>
</html>
<script type="text/javascript">
proinceChanged();
</script>


action 类:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.csjl.infopub.struts2.usr;

import com.opensymphony.xwork2.*;
/**
*
* @author Administrator
*/
public class RegisterAction extends ActionSupport{
private String username;
private String password1;
private String password2;
private String email;
private int province;
private int city;
private int area;

public String doRegister() throws Exception{
return "abc";
}

/**
* @return the username
*/
public String getUsername() {
return username;
}

/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}

/**
* @return the password1
*/
public String getPassword1() {
return password1;
}

/**
* @param password1 the password1 to set
*/
public void setPassword1(String password1) {
this.password1 = password1;
}

/**
* @return the password2
*/
public String getPassword2() {
return password2;
}

/**
* @param password2 the password2 to set
*/
public void setPassword2(String password2) {
this.password2 = password2;
}

/**
* @return the email
*/
public String getEmail() {
return email;
}

/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}

/**
* @return the province
*/
public int getProvince() {
return province;
}

/**
* @param province the province to set
*/
public void setProvince(int province) {
this.province = province;
}

/**
* @return the city
*/
public int getCity() {
return city;
}

/**
* @param city the city to set
*/
public void setCity(int city) {
this.city = city;
}

/**
* @return the area
*/
public int getArea() {
return area;
}

/**
* @param area the area to set
*/
public void setArea(int area) {
this.area = area;
}
}


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>

<include file="struts-default.xml" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<package name="user-manage" namespace="/usr" extends="struts-default">
<action name="register" class="com.csjl.infopub.struts2.usr.RegisterAction" method="doRegister">
<result name="input">/usr/register1.jsp</result>
<result name="success">/usr/register1.jsp</result>
</action>
</package>
</struts>


小灰狼 2010-09-21
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 liuchao1989 的回复:]
doRegister() 返回的是“abc”而你struts.xml文件中没有配置<result name="abc"> </result>,系统默认就找input.
[/Quote]

就算是有 name="abc" 的 result 也没用,无论怎么配置,它都跑到 name="input" 的 result 去
zhyou110z 2010-09-21
  • 打赏
  • 举报
回复

public String doRegister() throws Exception{

System.out.println("adc");
return "abc";
}
看有沒有輸出 要不你設置個断点你DEBUG下

return 的是你 STRUTS.XML ACTION里的 RESULT

看是不是其他验证的错误 默认是就转向 INPUT 的

liuchao1989 2010-09-21
  • 打赏
  • 举报
回复
doRegister() 返回的是“abc”而你struts.xml文件中没有配置<result name="abc"> </result>,系统默认就找input.
  • 打赏
  • 举报
回复
sb, action和xml牛头不对马嘴还说一大堆
tuo_bing 2010-09-21
  • 打赏
  • 举报
回复
是否做了验证 ? 验证不通过 ,则跳转到 了 input 的页面
yzsunlight 2010-09-21
  • 打赏
  • 举报
回复
楼主加油
小灰狼 2010-09-21
  • 打赏
  • 举报
回复
已经找到原因了

原来是在 json 中,原本应该是 aId 的,不小心写成了 aid,全部是小写。这样一来,“地区”的下拉框得到的 value 值将会是 undefined,而在 RegisterAction 的 area 属性是 int 型的,struts2 在调用 set 方法时失败,因此直接回到了 input 的 result
小灰狼 2010-09-21
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 llongfeng2008 的回复:]
你的<result name="input">/usr/register1.jsp</result>
<result name="success">/usr/register1.jsp</result>
都是register1.jsp一个页面你怎么区别是input还是success呢?
[/Quote]

这个我改过很多次,改起来也简单,可以把 register1.jsp 改成 1.htm, 2.htm, 3.htm, ...... n.htm

但是结果就是 struts2 会去找 name="input" 的 result
huan_lxyd 2010-09-21
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 tuo_bing 的回复:]
是否做了验证 ? 验证不通过 ,则跳转到 了 input 的页面
[/Quote]

应该是验证失败了吧。。。
加载更多回复(20)

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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