Skip to content

Commit

Permalink
恢复被删除的文件
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyliuxin committed Mar 7, 2017
1 parent 0276b95 commit e92de87
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
39 changes: 39 additions & 0 deletions liuxin/src/com/coderising/litestruts/LoginAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.coderising.litestruts;

/**
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。
* @author liuxin
*
*/
public class LoginAction{
private String name ;
private String password;
private String message;

public String getName() {
return name;
}

public String getPassword() {
return password;
}

public String execute(){
if("test".equals(name) && "1234".equals(password)){
this.message = "login successful";
return "success";
}
this.message = "login failed,please check your user/pwd";
return "fail";
}

public void setName(String name){
this.name = name;
}
public void setPassword(String password){
this.password = password;
}
public String getMessage(){
return this.message;
}
}
37 changes: 37 additions & 0 deletions liuxin/src/com/coderising/litestruts/Struts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.coderising.litestruts;

import java.lang.reflect.Method;
import java.util.Map;



public class Struts {


public static View runAction(String actionName, Map<String,String> parameters) {

/*
0. 读取配置文件struts.xml
1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象)
据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是
("name"="test" , "password"="1234") ,
那就应该调用 setName和setPassword方法
2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success"
3. 通过反射找到对象的所有getter方法(例如 getMessage),
通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} ,
放到View对象的parameters
4. 根据struts.xml中的 <result> 配置,以及execute的返回值, 确定哪一个jsp,
放到View对象的jsp字段中。
*/


return null;
}

}
43 changes: 43 additions & 0 deletions liuxin/src/com/coderising/litestruts/StrutsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.coderising.litestruts;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;





public class StrutsTest {

@Test
public void testLoginActionSuccess() {

String actionName = "login";

Map<String,String> params = new HashMap<String,String>();
params.put("name","test");
params.put("password","1234");


View view = Struts.runAction(actionName,params);

Assert.assertEquals("/jsp/homepage.jsp", view.getJsp());
Assert.assertEquals("login successful", view.getParameters().get("message"));
}

@Test
public void testLoginActionFailed() {
String actionName = "login";
Map<String,String> params = new HashMap<String,String>();
params.put("name","test");
params.put("password","123456"); //密码和预设的不一致

View view = Struts.runAction(actionName,params);

Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp());
Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message"));
}
}
23 changes: 23 additions & 0 deletions liuxin/src/com/coderising/litestruts/View.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.coderising.litestruts;

import java.util.Map;

public class View {
private String jsp;
private Map parameters;

public String getJsp() {
return jsp;
}
public View setJsp(String jsp) {
this.jsp = jsp;
return this;
}
public Map getParameters() {
return parameters;
}
public View setParameters(Map parameters) {
this.parameters = parameters;
return this;
}
}
11 changes: 11 additions & 0 deletions liuxin/src/com/coderising/litestruts/struts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<struts>
<action name="login" class="com.coderising.litestruts.LoginAction">
<result name="success">/jsp/homepage.jsp</result>
<result name="fail">/jsp/showLogin.jsp</result>
</action>
<action name="logout" class="com.coderising.litestruts.LogoutAction">
<result name="success">/jsp/welcome.jsp</result>
<result name="error">/jsp/error.jsp</result>
</action>
</struts>

0 comments on commit e92de87

Please sign in to comment.