struts2框架主要是封装了servlet,简化了jsp跳转的复杂操作,并且提供了易于编写的标签,可以快速开发view层的代码。
过去,我们用jsp和servlet搭配,实现展现时,大体的过程是:
1 jsp触发action
2 servlet接受action,交给后台class处理
3 后台class跳转到其他的jsp,实现数据展现
现在有了struts2,实现过程变为
1 jsp出发action
2 struts2拦截请求,调用后台action
3 action返回结果,由不同的jsp展现数据
需要的包如下:
1 commons-fileupload-1.3.2.jar 2 javassist-3.11.0.GA.jar 3 commons-io-2.2.jar 4 ognl-3.0.19.jar 5 commons-lang3-3.2.jar 6 struts2-core-2.3.32.jar 7 commons-logging-1.1.3.jar 8 xwork-core-2.3.32.jar 9 freemarker-2.3.22.jar
登陆页面代码:login.jsp
<%@page language="java" contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><s:text name="loginPage"/></title> </head> <body> <s:form action="login"> <!--s:textfield name="username" key="user"/--> <p> 输入用户名:<input type="text" name="username" key="user"/> </p> <!--s:textfield name="password" key="pass"/--> <p> 输入密码:<input type="password" name="password" key="pass"/> </p> <s:submit key="login"/> </s:form> </body> </html>
登陆成功页面:welcome.jsp
<%@page language="java" contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><s:text name="succPage"/></title> </head> <body> <s:text name="succTip"> <s:param>${sessionScope.user}</s:param> </s:text><br/> </body> </html>
登陆失败页面:error.jsp
<%@page language="java" contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><s:text name="errorPage"/></title> </head> <body> <s:text name="failTip"/> </body> </html>
处理action的java代码:LoginAction.java
package edu.jju; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; /** * * @author munication */ public class LoginAction extends ActionSupport { 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; } /** * * @return * @throws Exception */ @Override public String execute() throws Exception { if (getUsername().equals("admin") && getPassword().equals("xxl123")) { ActionContext.getContext().getSession().put("user", getUsername()); return SUCCESS; }else{ return ERROR; } } }
部署文件web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <session-config> <session-timeout> 30 </session-timeout> </session-config> <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> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
struts2的配置文件:struts.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <!-- 指定全局国际化资源文件 --> <!--constant name="struts.custom.i18n.resources" value="mess"/--> <!-- 指定国际化编码所使用的字符集 --> <!--constant name="struts.i18n.encoding" value="utf-8"/--> <!-- 所有的Action定义都应该放在package下 --> <package name="test" extends="struts-default"> <action name="login" class="edu.jju.LoginAction"> <result name="error">/error.jsp</result> <result name="success">/welcome.jsp</result> </action> </package> </struts>
至此第一个demo完成了。