struts2基础
- web浏览器请求一个资源
- 过滤器Dispatch查找请求,确定适当的Action
- 拦截器自动对请求应用通用功能,如验证和文件上传等操作
- Action的excute方法通常用来存储和(或)重新获得信息(数据库)
- 结果返回浏览器。可能是HTML,图片,PDF或其他。
struts搭建:
在web.xml文件配置struts过滤器 (struts本身就是一种过滤器),编写struts基本参数,访问前端控制器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 3 <display-name>e5</display-name> 4 <welcome-file-list> 5 <welcome-file>login.jsp</welcome-file> 6 </welcome-file-list> 7 <filter> 8 <filter-name>struts2</filter-name> 9 <filter-class>org.apache.struts2.dispacher.ng.filter.Struts.PrepareAndExcuntefilter</filter-class> 10 <init-param> 11 <param-name>actionPackages</param-name> 12 <param-value>com.mycompany.mapp.actions</param-value> 13 </init-param> 14 </filter> 15 <filter-mapping> 16 <filter-name>struts2</filter-name> 17 <url-pattern>/*</url-pattern> 18 </filter-mapping> 19 </web-app>
编写 jsp 其中使用struts的表单及其他标签,与HTML略有不同.,jsp页面中指定该页面访问哪个Action
实现Action 控制器,编写java类继承ActionSupport实现execute方法实现数据查询及返回相应的值来判断页面跳转。Action中可以有user属性并生成getter和setter方法。Action 的属性总是在调用execute方法之前被设置(通过getter,setter)所以不user属性是不需要手动调用getter,setter来设置实例后会从jsp自动的获得对象,所以在execute中可以直接使用。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.sql.ResultSet; 2 import java.util.Map; 3 4 import com.opensymphony.xwork2.ActionContext; 5 import com.opensymphony.xwork2.ActionSupport; 6 import com.opensymphony.xwork2.Result; 7 8 public class MainAction extends ActionSupport { 9 private UserTable user; 10 11 public UserTable getUser() { 12 return user; 13 } 14 public void setUser(UserTable user) { 15 this.user = user; 16 } 17 @Override 18 public String execute() throws Exception { 19 String username= user.getName(); 20 String password= user.getPassword(); 21 boolean validated = false; 22 SqlSrvDBConn sqlSrvDBConn = new SqlSrvDBConn(); 23 ActionContext context = ActionContext.getContext(); 24 Map session = context.getSession(); 25 UserTable userl = null; 26 userl=(UserTable)session.get("user"); 27 if (userl==null) { 28 String sql = "select * from userTable"; 29 ResultSet rs = sqlSrvDBConn.executeQuery(sql); 30 31 try { 32 while(rs.next()){ 33 if ((rs.getString(username).trim().compareTo(username)==0)&&(rs.getString("password").compareTo(password)==0)) { 34 userl = new UserTable(); 35 userl.setId(rs.getInt(1)); 36 userl.setName(rs.getString(2)); 37 userl.setPassword(rs.getString(3)); 38 session.put("user", userl); 39 validated = true; 40 } 41 } 42 rs.close(); 43 } catch (Exception e) { 44 e.printStackTrace(); 45 } 46 sqlSrvDBConn.close(); 47 }else { 48 validated = true; 49 } 50 if (validated) { 51 return "success"; 52 }else { 53 return "error"; 54 } 55 } 56 57 }
在src下创建struts.xml文件配置Action,根据Action的返回值分配相应的页面跳转
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 <struts> 6 <package name="default" extends="struts-default"> 7 <!-- 用户登录 --> 8 <action name="main" class="com.struts.MainAction"> 9 <result name="success">/mmain.jsp</result> 10 <result name="error">/error.jsp</result> 11 </action> 12 </package> 13 <constant name="struts.i18n.encoding" value="UTF-8" ></constant> 14 </struts>