1、jsp页面 输入框,提交按钮
1 <%@ page language="java" contentType="text/html" pageEncoding="utf-8"%> 2 <%@ taglib uri="/struts-tags" prefix="s" %> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <title>Insert title here</title> 7 </head> 8 <body> 9 <s:form action="User_login" namespace="/login"> 10 <!---token标签---> 11 <s:token></s:token> 12 <!-- <!---用于显示action的错误,因为设置的是拦截到后再次返回此页,所以设置了这个标签--> 13 <!-- <s:actionerror/> --> 14 15 <s:textfield name="username" label="用户名"></s:textfield> 16 <s:submit label="提交"></s:submit> 17 </s:form> 18 </body> 19 </html>
2、struts.xml
动作类成功执行后通过转发的方式跳转到success.jsp页面。
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 3 <struts> 4 <constant name="struts.i18n.encoding" value="utf-8"/> 5 <package name="default" namespace="" extends="struts-default"> 6 <global-results> 7 <result name="error">/error.jsp</result> 8 </global-results> 9 </package> 10 11 <package name="users" extends="default" namespace="/login"> 12 <action name="User_login" class="cn.gs.ly.UserAction" method="login"> 13 14 <!--使用默认拦截器--> 15 <interceptor-ref name="defaultStack"></interceptor-ref> 16 <!--使用token拦截器--> 17 <!-- <interceptor-ref name="token"></interceptor-ref> --> 18 <!--使用tokenSession拦截器--> 19 <interceptor-ref name="tokenSession"></interceptor-ref> 20 <!--拦截到后的输出界面--> 21 <result name="invalid.token">/message.jsp</result> 22 23 <result name="success">/User_login_success.jsp</result> 24 <result name="failure">/index.jsp</result> 25 </action> 26 </package> 27 28 </struts>
3、UserAction类
每次用户提交表单后,输出表单提交的提示。
1 package cn.gs.ly; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class UserAction extends ActionSupport { 6 private String username; 7 private String token; 8 9 public String getUsername(){ 10 return username; 11 } 12 public void setUsername(String username){ 13 this.username=username; 14 } 15 public String getToken() { 16 return token; 17 } 18 public void setToken(String token) { 19 this.token = token; 20 } 21 22 23 public String login(){ 24 System.out.println(username+"表单已提交"); 25 return SUCCESS; 26 } 27 28 29 }
登陆成功跳 success.jsp
1 <%@ page language="java" contentType="text/html" pageEncoding="utf-8"%> 2 <!DOCTYPE html> 3 <html> 4 <head> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 <h2>登陆成功,这是登陆成功页面</h2> 9 </body> 10 </html>
重复提交跳 message.jsp
<%@ page language="java" contentType="text/html" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <h3>请勿重复提交</h3> <!---用于显示action的错误,因为设置的是拦截到后再次返回此页,所以设置了这个标签--> <s:actionerror/> </body> </html>
登陆失败跳 error.jsp
1 <%@ page language="java" import="java.util.*" isErrorPage="true"%> 2 <%@ page contentType="text/html; charset=utf-8" %> 3 <%@ taglib uri="/struts-tags" prefix="s"%> 4 <% 5 String path = request.getContextPath(); 6 %> 7 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <title>page</title> 12 </head> 13 <body> 14 <center> 15 <h1>出错了!</h1> 16 <hr> 17 异常信息:<s:property value="exception"/><br> 18 <a href="<%=path%>/index.jsp">返回首页</a> 19 </center> 20 </body> 21 </html>