之前在阅读别人的项目时,总感觉结构非常相似,我也听过javaee的三大框架,不过学校貌似没有这方面的课程,自己也一直没有时间学习。终于,在图书馆借了本《疯狂struts2.X》。粗粗看了下入门章节(第二章),完成了简单的struts架构的登录界面。在此做个总结。
一,实现控制器类
package com.struts.action; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.struts.service.BookService; public class getBookAction extends ActionSupport {//ActionSupport类已经实现了Action接口,并提供一个validate方法,这个方法会在execute方法之前调用,提供数据校验 private String[] books; private String username; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public String execute() throws Exception {//重写execute方法 // TODO Auto-generated method stub if (username.equals("lidian") && password.equals("123456")) ActionContext.getContext().getSession().put("user", username);//把用户信息添加到HTTP session中 String user = (String) ActionContext.getContext().getSession() .get("user"); if (user != null && user.equals("lidian")) { BookService bs = new BookService(); this.setBooks(bs.getBooks()); // ActionContext.getContext(). return this.SUCCESS; } else return this.ERROR; } @Override public void validate() {//先调用次方法 if (this.getUsername() == null || this.getUsername().trim().equals("")) { this.addFieldError("username", "不能为空!"); } if (this.getPassword() == null || this.getPassword().trim().equals("")) { this.addFieldError("password", "密码不为空!"); } } public String[] getBooks() { return books; } public void setBooks(String[] books) { this.books = books; } }
二,配置Action
上面的Action还未配置在web应用中,不能处理用户请求,通过struts.xml配置。struts.xml用来定义Action的处理结果和资源的映射关系。用eclipse的话,把此文件放在src目录下即可。注意DOCTYPE部分使用的dtd的版本和使用的struts的版本要对应(我用的struts2.1),否则这个struts是无法加载的
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <!-- <constant name="struts.devMode" value="true" /> --> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <action name="login" class="com.struts.action.getBookAction"> <result name="input"> /index.jsp </result> <result name="success"> /WEB-INF/welcome.jsp<!--把jsp页面放在web-INF目录下保护起来--> </result> <result name="error"> /WEB-INF/error.jsp </result> </action> </package> <!-- <include file="example.xml"/> --> <!-- Add packages here --> </struts>
三,添加Model
该业务逻辑组件负责实现系统业务逻辑方法,返回一个静态的字符数组,该数组模拟持久层组件,呵呵。
package com.struts.service; public class BookService { private String[] books = { "book1", "book2", "book3" }; public String[] getBooks() { return books; } }
四,配置web.xml
<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>
五,配置视图
这里使用jsp作为视图页面,在jsp页面中控制输出实在太乱,真实开发也一般不用,这里使用struts标签改善一下。省略部分简单的jsp
1,登录页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html;charset=gb2312"> <title>登录页面</title> </head> <body> <s:form action="login"> <s:textfield name="username" label="用户名"></s:textfield> <s:textfield name="password" label="密码"></s:textfield> <s:submit value="登录"></s:submit> </s:form> </body> </html>
2,输出页面
<%@ page language="java" import="java.util.*,com.opensymphony.xwork2.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html;charset=gb2312"> <title>登录页面</title> </head> <body> <table border="1" width="360"> <s:iterator value="books" status="index"> <s:if test="#index.odd == true"> <tr style="background-color: #cccccc"> </s:if> <s:else> <tr> </s:else> <td>书名:</td> <td><s:property /></td> </tr> </s:iterator> </table> </body> </html>
六,使用eclipse的话直接部署到tomcat中。
这里只是主要步骤,省略部分次要步骤