Struts2,get/set 自动获取/设置数据ActionSupport 类
主页:http://struts.apache.org/
在用户请求和模块化处理方面以及页面的展现这块,Struts2 发挥了强大的作用;
相对于传统的Jsp+Servlet 模式,Struts2 更适合企业级团队开发,方便系统的维护;
Struts2 HelloWorld
<filter> <filter-name>Struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>Struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Action 设置数据
第一种方式:属性驱动(FieldDriven)
A、基本数据类型属性
B、JavaBean 类型属性
第二种方式:模型驱动(ModelDriven)
基本数据类型属性
<%@ 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>登录</title> </head> <body> <form action="login" method="post"> 用户名:<input type="text" name="userName" id="userName"/><br><br> 密码:<input type="password" name="password" id="password"/><br><br> <input type="submit" value="登录" /><input type="reset" value="重置" /> </form> </body> </html>
<struts> <package name="welcomeToZiBo" extends="struts-default"> <action name="login" class="com.zhiqi.action.UserAction"> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action> </package> </struts>
package com.zhiqi.action; import com.opensymphony.xwork2.ActionSupport; import com.zhiqi.model.User; import com.zhiqi.service.UserService; 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("开始"); User user=new User(); user.setUserName(userName); user.setPassword(password); if(userService.login(user)){ return SUCCESS; }else{ return ERROR; } } }
package com.zhiqi.service; import com.zhiqi.model.User; public class UserService { public boolean login(User user){ if("web".equals(user.getUserName())&& "123".equals(user.getPassword())){ return true; }else{ return false; } } }
JavaBean 类型属性
<form action="login" method="post"> 用户名:<input type="text" name="user.userName" id="userName"/><br><br> 密码:<input type="password" name="user.password" id="password"/><br><br> <input type="submit" value="登录" /><input type="reset" value="重置" /> </form>
<action name="login" class="com.zhiqi.action.UserAction2"> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action>
package com.zhiqi.action; import com.opensymphony.xwork2.ActionSupport; import com.zhiqi.model.User; import com.zhiqi.service.UserService; 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 { if(userService.login(user)){ return SUCCESS; }else{ return ERROR; } } }
模型驱动(ModelDriven)
<form action="login" method="post"> 用户名:<input type="text" name="userName"/> 密码:<input type="text" name="password"/> <input type="submit" value="登录"/> </form>
<action name="login" class="com.zhiqi.action.UserAction3"> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action>
package com.zhiqi.action; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.zhiqi.model.User; import com.zhiqi.service.UserService; 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 { if(userService.login(user)){ return SUCCESS; }else{ return ERROR; } } @Override public User getModel() { // TODO Auto-generated method stub return user; } }
struts.xml 配置
一,pageckage 配置
name 包名
extends 继承
namespace 包命名空间
abstract 抽象包
二,action 配置
name action 名
class 处理类
method 方法
三,分模块配置方法
<include file="" ></include>