struts2入门-第一个例子

1.从apache官网下载struts2(v2.2.1)的压缩文件,解压缩

2.打开myeclipse(v8.6),将struts2解压缩后lib下的ongl,strtus2-core,xwork-core,commons-logging,freemaker,javassist(该包需要另外下载,老版本里不需要)这六个必须包导入

在struts2(v2.2.3.1)除了上面包外还需导入commons-io,commons-lang,commons-fileupload

3.配置web.xml(WEB-INF下)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.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>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

老版本中用的过滤器是Dispatch类

4.编写用户登录表单

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  	
  <body>
   <form action="Login" method="post">
  	
	<Table>
		<Tr>
			<td>用户名:</Td>
			<td><input type="text" name="username" /></Td>
		</Tr>
		<Tr>
			<td>密码:</Td>
			<td><input type="password" name="password" /></Td>
		</Tr>
		<Tr>
			<td><input type="submit" name="submit" value="提交"/></Td>
			<td><input type="reset" name="reset" value="重置"/></Td>
		</Tr>
	</Table> 
  	
  	
  	</form>
  </body>
</html>

5.编写LoginAction.java

package com.struts2.action;

public class LoginAction {
	private String username;
	private String password;
	
	public String execute() throws Exception
	{
		if(this.getUsername().equals("tom")&&this.getPassword().equals("111111")){
			return "success";
		}else{
			return "error" ;
		}
	}

	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;
	}
	
	
	
}

6.配置strtus.xml(src下)

<?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="strutsqs"  extends="struts-default">
		<action name="Login" class="com.struts2.action.LoginAction">
			<result name="success">/welcome.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>

7.结果页面就一句话,就不写了

你可能感兴趣的:(struts2入门-第一个例子)