学习Struts弄了一整天才弄好的一个简单的登录demo
中间大部分时间都在处理bug,各种报错,然后慢慢去理解试着去解决,还是一路报错,
最后索性思路理解清楚了,开始自己写,不在参考网络上的教程,结果就ok了!
记录这篇日志,算是学习框架终于迈出了一步,一个礼拜了各种难以理解,现在终于摸到了,加油,笨鸟总会飞起来的,只要坚持!
-------------------------分---割---线----------------------------------
实现LoginAction类
package com.dong1990.web.helloworld;
public class LoginAction {
private User user;
public User getUser() {
System.out.println("LoginAction1 -- getUser()");
return user;
}
public void setUser(User user) {
System.out.println("LoginAction2 -- setUser(User user)");
this.user = user;
}
public String verify(){
if("admin".equals(user.getUsername()) && "123".equals(user.getPassword())){
System.out.println("LoginAction3 -- verify() -- yes");
return "yes";
}
System.out.println("LoginAction4 --verify() -- error");
return "error";
}
}
定义User类
package com.dong1990.web.helloworld;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
@Override
public String toString() {
System.out.println("User1 -- toString()");
return "User [username=" + username + ", password=" + password + "]";
}
public void setUsername(String username) {
System.out.println("User2 -- setUsername(String username)");
this.username = username;
}
public String getPassword() {
System.out.println("User3 -- getPassword()");
return password;
}
public void setPassword(String password) {
System.out.println("User4 -- setPassword(String password)");
this.password = password;
}
}
设置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.objectFactory" value="spring"/> -->
<package name="default" extends="struts-default">
<action name="hello" class="com.dong1990.web.helloworld.Helloworld" method="sayHello">
<result name="success">/success.jsp</result>
</action>
<action name="login" class="com.dong1990.web.helloworld.LoginAction" method="verify">
<result name="yes">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
web.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>aa</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
error.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
your login is error , login again please !
<a href="login.jsp">back</a>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
<table>
<tr>
<td>username:</td>
<td><input type="text" name="user.username"></td>
</tr>
<tr>
<td>pasword:</td>
<td><input type="password" name="user.password"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
execute success<br/>
welcome to <s:property value="user.username"/>,login success !
<s:property value="msg"/>
</body>
</html>
action传值的顺序:
注意在表单name属性的书写格式:user.username
。但是我们在LoginAction中并未使用任何初始化的方法构造User对象,那么值是怎么传递过来的呢?在Struts2中使用了自动绑定的机制,当LoginAction中获取user.username
属性的时候,实际上会执行以下过程:
action.getUser();
//当Struts2发现获取的User对象是null的时候,调用以下的语句
action.setUser(new User());
//通过调用User类的默认构造方法,实例化User对象之后,就可以设置username的属性的值了
action.getUser().setUserName();