struts2.0起步
想看第一个简单的struts2.0项目
1、新建项目,引入struts相关的jar包
2、配置web.xml和struts.xml文件
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <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>
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> <package name="default" extends="struts-default"> <action name="login" class="com.itime.action.LoginAction" method="Test"> <result name="success">/welcome.jsp</result> <result name="login">/login.jsp</result> </action> </package> </struts>
3、登录界面login.jsp 和处理action及登录成功页面
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<s:form action="login" method="post">
<s:label value="系统登陆"></s:label>
<s:textfield name="username" label="账号" />
<s:password name="password" label="密码" />
<s:submit value="登录" />
</s:form>
</body>
</html>
LoginAction
package com.itime.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private static final long serialVersionUID = 1661426853473626469L; 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; } @Override public String execute() throws Exception { if ("邹腾".equals(username) && "admin".equals(password))// 如果登录的用户名=haha并且密码=hehe,就返回SUCCESS;否则,返回LOGIN { return SUCCESS; } else { return LOGIN; } } public String Test() throws Exception { if ("邹腾".equals(username) && "admin".equals(password))// 如果登录的用户名=haha并且密码=hehe,就返回SUCCESS;否则,返回LOGIN { return SUCCESS; } else { return LOGIN; } } }
welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>welcome</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> welcome,欢迎 欢迎${username }!来到struts2 ,密码是 ${password}; </body> </html>