1. Struts2 getter/setter 自动获取/设置参数

过一下Struts2-2_html


上述前端使用GET方法。POST方法也可以:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="hello" method="post">
name:<input type="text" name="name"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>

2. ActionSupport类的引入

public class HelloWorldAction2 extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 1L;
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String execute() throws Exception {
System.out.println("执行了HelloWorldAction2 Action的默认方法");
return SUCCESS;
}

}

ActionSupport的实现:

public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {
......

一般开发是使用ActionSupport类而不是Action接口,效果一样。

3. Action设置数据

  • 属性驱动(FieldDriven)
  1. 基本数据类型属性

关键代码如下:

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="helloworld" extends="struts-default">
<!-- 请求 /hello -->
<action name="hello" class="com.bee.action.HelloWorldAction">
<result name="success">helloworld.jsp</result>
</action>

<action name="user" class="com.bee.action.UserAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>

User.java

public class User {

private String userName;
private String password;

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;
}

}

UserAction .java

public class UserAction extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 1L;

private UserService userService = new UserService();

private String userName;
private String password;

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;
}

@Override
public String execute() throws Exception {
System.out.println("执行了UserAction的默认方法");
User user = new User();
user.setUserName(userName);
user.setPassword(password);
if (userService.login(user)) {
return SUCCESS;
} else {
return ERROR;
}
}

}

UserService.java

public class UserService {

public boolean login(User user) {
if ("bee".equals(user.getUserName()) && "123456".equals(user.getPassword())) {
return true;
} else {
return false;
}
}
}

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="user" method="post">
用户名:<input type="text" name="userName"/>
密码:<input type="text" name="password"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
  1. JavaBean类型属性

关键代码如下:

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="helloworld" extends="struts-default">
<action name="user2" class="com.bee.action.UserAction2">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>

UserAction2.java

public class UserAction2 extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 1L;

private UserService userService = new UserService();

private User user;

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

@Override
public String execute() throws Exception {
System.out.println("执行了UserAction的默认方法");
if (userService.login(user)) {
return SUCCESS;
} else {
return ERROR;
}
}

}

login2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="user2" method="post">
用户名:<input type="text" name="user.userName"/>
密码:<input type="text" name="user.password"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
  • 模型驱动(ModelDriven)
    只接收一个JavaBean。

关键代码:

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="helloworld" extends="struts-default">
<action name="user3" class="com.bee.action.UserAction3">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>

UserAction3.java

public class UserAction3 extends ActionSupport implements ModelDriven<User> {

/**
*
*/
private static final long serialVersionUID = 1L;

private UserService userService = new UserService();

private User user = new User();

@Override
public String execute() throws Exception {
System.out.println("执行了UserAction3的默认方法");
if (userService.login(user)) {
return SUCCESS;
} else {
return ERROR;
}
}

@Override
public User getModel() {
return user;
}

}

login3.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="user3" method="post">
用户名:<input type="text" name="userName"/>
密码:<input type="text" name="password"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>