Struts实战演练

it2022-06-08  89

一 Struts2的下载地址

https://struts.apache.org/download.cgi#struts2520

下载:struts-2.3.31-lib.zip

二 实战

1 创建Struts工程StrutsDemo

2 在Eclipse中部署Struts开发包

首先将Struts开发包拷贝到lib下

三 创建模型类

package com.demo; public class loginModel { private String msgSuccess; private String msgError; public loginModel(String success, String error){ this.msgSuccess = success; this.msgError = error; } public String getSuccessMessage() { return msgSuccess; } public void setSuccessMessage(String message) { this.msgSuccess = message; } public String getErrorMessage() { return msgError; } public void setErrorMessage(String message) { this.msgError = message; } }

四 创建行为类

package com.demo; import com.opensymphony.xwork2.ActionSupport; public class loginAction extends ActionSupport { private static final long serialVersionUID = 1L; private String userName; // action属性 private String password; // action属性 private loginModel loginMsg; public String execute() throws Exception{ loginMsg = new loginModel("登录成功! ","登录失败! ") ; if (userName.equals("cakin24") && password.equals("123456")) { return "success"; // 如果密码账号密码正确,跳到success.jsp页面 } else { return "error"; // 如果密码账号密码不正确,跳到error.jsp页面 } } public String getUserName() { // getter方法 return userName; } public void setUserName(String userName) { // setter方法 this.userName = userName; } public String getPassword() { // getter方法 return password; } public void setPassword(String password) { // setter方法 this.password = password; } public loginModel getLoginMsg() { // getter方法 return loginMsg; } public void setLoginMsg(loginModel loginMsg) { // setter方法 this.loginMsg = loginMsg; } }

Action类返回一个标准的字符串,该字符串是一个逻辑视图名,该视图名对应实际的物理视图,该方法返回一个字符串,不同的字符串,代表不同的结构,拦截这样的字符串,就可以执行不同的动作。

返回不同的字符串,我们要找对应URL来处理,就需要下面的struts.xml来做对应的配对映射。

五 创建视图

1 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!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>Struts Demo!</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 success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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> <h2><s:property value="loginMsg.getSuccessMessage()"/> 欢迎 <s:property value="userName"/></h2> </body> </html>

3 error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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> <h2><s:property value="loginMsg.getErrorMessage()"/></h2> </body> </html>

六 编写工程配置文件

1 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" 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>strutsLoginDemo</display-name> <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>index.jsp</welcome-file> </welcome-file-list> </web-app>

2 struts.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="com.demo.loginAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>

七 运行

 


最新回复(0)