搭建Struts2环境

1、要先下载Struts2的jar包,核心包:common-loggin-1.0.4.jar,freemarker-2.3.8.jar,ognl-2.6.11.jar,struts2-core-2.0.14.jar,xwork-2.0.7.jar(版本可能不同),将这些包复制到WEB-INF/lib文件夹下。

2 、编辑web.xml,配置struts2核心Filter。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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>
</web-app>


3、建立一个简单提交请求页面
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
  <head>
    <html:base />
    
    <title>test.jsp</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.action" method="post">
   		<table align="center">
   			<tr><td>用户名<input type="text" name="username" /> </td></tr>
   			<tr><td>密码<input type="text" name="password" /></td></tr>
   			<tr><td colspan="2"><input type="submit" value="登录" /> </td></tr>
   		</table>
   	</form>
  </body>
</html:html>


4、创建Action
public class LoginAction {

	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;
	}
	
	public String execute(){
		
		if("admin".equals(getUsername())&&"admin".equals(getPassword())){
			return "success";
		}
		else{
			return "error";
		}
	}
	
}


5、在src目录下创建一个struts.xml文件,将Action配置在web应用中
<?xml version="1.0" encoding="UTF-8"?>
	<!-- 指定Struts 2配置文件的DTD信息 -->

<!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<package name="struts2test" extends="struts-default">
		<action name="Login" class="com.struts2.test.LoginAction">
			<result name="success">/welcome.jsp</result>
			<result name="error">/login.jsp</result>
		</action>
	</package>
</struts>


6、增加测试跳转成功页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title></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>
  登陆成功<br/>
   欢迎你:${username }
  </body>
</html>


启动Tomcat。运行http://localhost:8080/struts2/login.jsp

你可能感兴趣的:(java,apache,tomcat,xml,struts)