开发者社区> 问答> 正文

Struts 的Action问题?报错

编写了一个简单的登录页面,在action进行校验,然后在页面中有一个


<div><font color="red"><s:actionerror /></font></div>

用来struts提示错误信息使用,下面是action中的方法:

String code = (String) ServletActionContext.getRequest().getSession().getAttribute("checkcode");
		if(code.equals(checkcode)) {
			List<User> list = userService.login(loginname, password);
			if(list == null || list.size() == 0) {
				this.addActionError("用户名或密码错误!");
				return LOGIN;
			}
			ServletActionContext.getRequest().getSession().setAttribute("user", list.get(0));
			List<Menu> menuList =  (List<Menu>) ServletActionContext.getRequest().getSession().getAttribute("menuList");
			if(menuList == null) {
				menuList = menuService.getNeed(0);
				ServletActionContext.getRequest().getSession().setAttribute("menuList", menuList);
			}
		}else {
			this.addActionError("验证码错误!");
			return LOGIN;
		}
		return "loginSuccess";



struts中的配置:

<action name="user_*" class="userAction" method="{1}">
			<result name="login">/jsp/login.jsp</result>
			<result name="loginSuccess">/jsp/index.jsp</result>
			<result name="input">/jsp/login.jsp</result>
		</action>
		
		<action name="index" class="indexAction">
			<result>/jsp/login.jsp</result>
		</action>



然后,当我访问index.action,跳转到login.jsp,第一次点击表单提交的时候,是可以成功的运行,如果信息都正确,正确跳转到相应页面, 如果有错误,错误信息也正确显示,但是显示了错误信息之后,页面运行了user_login.action,因为错误所以依然返回到login.jsp,再次填写表单,就会一直报错,一直显示刚刚的错误信息,地址栏是user_login.action,请问这个应该怎么解决?






展开
收起
爱吃鱼的程序员 2020-06-09 11:54:34 610 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    Action上面要用@Scope("prototype"),使用单例模式会出现错误.

    <spanstyle="color:#333333;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;line-height:24px;background-color:#F5F5F5;"><result<spanstyle="font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;font-size:14px;line-height:15.4px;background-color:#FFFFFF;">name="login"<spanstyle="color:#333333;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;line-height:24px;background-color:#F5F5F5;">type="redirect">/jsp/login.jsp</result>

    <spanstyle="color:#333333;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;line-height:24px;background-color:#F5F5F5;">这里的跳转类型改成重定向的方式跳转

    不行,我感觉是不是缓存的原因<resultname="login"type="redirect">/jsp/login.jsp</result><!--重定向到下面的index.action--><resultname="login"type="redirect">index</result>这两种配置都不可以,后台打了断点,方法就不会再进去了.相当于只进入一次.userAction的整个类贴出来看看终于解决了因为spring给Action的bean是单例模式,改成prototype就可以.回复<aclass='referer'target='_blank'>@丶KKdo:提交一次错误之后,用浏览器看看此时的aciton是指向哪的,如果正常指向不应该进不到login里啊不可以,不会再进入user_login.action了回复<aclass='referer'target='_blank'>@丶KKdo:信息错误之后再填写提交可以执行到login方法里么是的呀,第一次提交能进入这个方法执行,成功了也能成功跳转,失败了也能显示错误信息,但是再填信息就不会变了.<preclass="brush:java;toolbar:true;auto-links:false;">packagecom.auth.action;importjava.util.List;importjavax.annotation.Resource;importorg.apache.struts2.ServletActionContext;importorg.springframework.stereotype.Component;importcom.auth.entity.Menu;importcom.auth.entity.User;importcom.auth.service.MenuService;importcom.auth.service.UserService;importcom.opensymphony.xwork2.ActionSupport;@ComponentpublicclassUserActionextendsActionSupport{publicStringloginname;publicStringpassword;privateStringcheckcode;publicStringgetCheckcode(){returncheckcode;}publicvoidsetCheckcode(Stringcheckcode){this.checkcode=checkcode;}publicStringgetLoginname(){returnloginname;}publicvoidsetLoginname(Stringloginname){this.loginname=loginname;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}@ResourceprivateUserServiceuserService;@ResourceprivateMenuServicemenuService;publicStringlogin(){Stringcode=(String)ServletActionContext.getRequest().getSession().getAttribute("checkcode");if((code.toUpperCase()).equals((checkcode.toUpperCase()))){List<User>list=userService.login(loginname,password);if(list==null||list.size()==0){this.addActionError("用户名或密码错误!");returnLOGIN;}ServletActionContext.getRequest().getSession().setAttribute("user",list.get(0));List<Menu>menuList=(List<Menu>)ServletActionContext.getRequest().getSession().getAttribute("menuList");if(menuList==null){menuList=menuService.getNeed(0);ServletActionContext.getRequest().getSession().setAttribute("menuList",menuList);}}else{this.addActionError("验证码错误!");returnLOGIN;}return"loginSuccess";}}

    打了断点了,第一次启动服务的时候可以进入方法,然后如果登录失败重新登录,就不会再进入这个方法了.



    2020-06-09 11:54:52
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载