今天用struts2写了一个简单的登录操作;将步骤写出来;
1、写登录页面;
之前我还傻傻的用ajax来实现按钮的提交操作,后来一直不能实现;百度了一下说ajax是异步无跳转的操作,他的意思就是说包含了ajax的页面是不会进行页面跳转的,即使我们采用了struts2进行跳转,他也不会进行跳转。所以我最终采用了form表单的方式进行数据提交及页面跳转的操作;针对用户名,密码处,需写上name属性,这样才可以将数据从前台传到后台;
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 chaset="UTF-8"></meta>
<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">
-->
<!-- <script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
function tijiao(){
console.log("username:"+$("#username").val()+",password:"+$("#password").val());
$.get("denglu.action?username="+$("#username").val()+"&password="+$("#password").val(),function(data,status,xxx){
//alert("status:"+status);
if(status=="success"){
alert("回来了");
var s=${message}
alert("s:"+s);
}});
}
</script> -->
</head>
<body>
<form id="form1" action="denglu" method="post">
<table>
<tr>
<td>用户名</td>
<td><input id="username" type="text" name="username"></input>
</td>
</tr>
<tr>
<td>密码</td>
<td><input id="password" type="password" name="password"></input>
</td>
</tr>
<tr>
<td><input type="submit"></input>
</td>
</tr>
</table>
<span id="span">${message}</span>
</form>
</body>
</html>
2、写struts.xml配置文件;
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" extends="struts-default" namespace="/"> <action name="denglu" class="action.Login" method="execute"> <result name="success">welcom.jsp</result> <result name="faile">index.jsp</result> </action> </package> </struts>
3、写action类
在此,action中无需进行数据提交,他可以通过属性的get,set方法获取页面中的数据,而当我们需要在前台获取action中的参数时,那么该参数也必须提供一个get方法(set方法可以不提供)
package action;
import com.opensymphony.xwork2.ActionSupport;
/**
* 需要繼承ActionSupport
*
* @author Administrator
*
*/
public class Login extends ActionSupport {
private String username;
private String password;
private String message;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() {
System.out.println("有没有走到这里来");
String s;
System.out.println("getUsername():" +getUsername()+ ";password:" + getPassword());
if (getUsername().equals("admin") && getPassword().equals("a")) {
System.out.println("账号密码正确了");
message = "欢迎您," + username;
s = "success";
} else {
System.out.println("账号密码错误了");
message = "用户名或密码输入错误";
s = "faile";
}
System.out.println("message:" + message);
return s;
}
/**
* 要想获取action中的参数,必须给该变量设置get,set方法
* @return
*/
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
4、最后登录成功的页面
采用${value}的方式获取后台的数据
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'welcom.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">
-->
<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
<body>
${message}
</body>
</html>
5、测试的结果
转载于:https://blog.51cto.com/11428119/1792077