struts2入门程序
1、示例
搭建编程环境就先不说了,这里假设已经搭建好了编程环境,并且下好了strut2的jar包,接下来程序。
1.1 新建web项目
点击File->New->Dynamic Web Project
填写项目名称,一路Next,最后Finish,然后一个项目就建成了。
1.2 导入jar包,将下载的strut2文件夹下lib文件夹中的这5个jar拷贝到WEB-INF下的lib文件夹下
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.5.jar
freemarker-2.3.18.jar
javassist-3.11.0.GA.jar
ognl-3.0.4.jar
struts2-core-2.3.1.2.jar
xwork-core-2.3.1.2.jar
1.3 创建java类,先在scr包下创建一个com包,然后再在com包下创建一个java类
package com; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; String usrName; String passWord; String Msg; public String usrLogin() throws Exception{ if ( "ztq".equals(usrName) && "ztq".equals(passWord)){ Msg = "欢迎成功,欢迎"+usrName; return SUCCESS; }else{ Msg = "用户名或密码错误,请重新输入!"; return ERROR; } } public String getUsrName() { return usrName; } public void setUsrName(String usrName) { this.usrName = usrName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } public String getMsg() { return Msg; } public void setMsg(String msg) { Msg = msg; } }
1.4 在WebContent目录下新建文件夹pages,再在pages下面新建三个jsp页面(Index.jsp、Error.jsp、Success.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!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>Index Page</title> </head> <body> <s:form action="ztq/login.do"> <label>用户名:</label><input id=usrName name=usrName tabindex="1"/> <br/> <label>密码:</label><input id=passWord name=passWord type="password" tabindex="2"/> <br/> <input type="submit" value="登陆"/> </s:form> </body> </html>
<%@ 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>Error</title> </head> <body> ${Msg} </body> </html>
<%@ 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>Welcome</title> </head> <body> ${Msg} </body> </html>
1.5 在src包下新建配置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> <constant name="struts.i18n.encoding" value="UTF-8"/> <constant name="struts.action.extension" value="action,do"></constant> <package name="ztq" namespace="/ztq" extends="struts-default"> <action name="login" class="com.LoginAction" method="usrLogin"> <result name="success">/pages/Success.jsp</result> <result name="error">/pages/Error.jsp</result> </action> </package> </struts>
1.6 在WebContentWEB-INF目录下新建配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SSH</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>/pages/Index.jsp</welcome-file> </welcome-file-list> </web-app>
最后如下图:
然后运行程序
输入用户名ztq和密码ztq后
否则