struts-2.5.14.1-all.zip 下载后文件夹说明
apps:war格式的例子文件
lib:引用jar包文件
src:源码文件
docs:帮助文档
小例子:
1.创建web工程:struts
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app id="starter" version="2.4" 3 xmlns="http://java.sun.com/xml/ns/j2ee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 6 7 <!-- START SNIPPET: filter --> 8 <filter> 9 <filter-name>struts2</filter-name> 10 <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> 11 </filter> 12 <!-- START SNIPPET: filter --> 13 14 <filter-mapping> 15 <filter-name>struts2</filter-name> 16 <url-pattern>/*</url-pattern> 17 </filter-mapping> 18 19 <!-- Welcome file lists --> 20 <welcome-file-list> 21 <welcome-file>login.jsp</welcome-file> 22 </welcome-file-list> 23 </web-app>
2.页面文件 login.jsp
<%@ 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 'login.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.action"> username: <input type="text" name="username"><br> password: <input type="password" name="password"><br> age: <input type="text" name="age"><br> date: <input type="text" name="date"><br> <input type="submit" value="submit"> </form> </body> </html>
3.Struts配置文件(struts.xml默认是在src文件夹下)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 4 "http://struts.apache.org/dtds/struts-2.5.dtd"> 5 <struts> 6 7 <package name="struts2" extends="struts-default"> 8 <action name="login" class="com.shengsiyuan.struts2.LoginAction"> 9 <result name="success">/result.jsp</result> 10 </action> 11 </package> 12 13 </struts>
4.调用文件
1 package com.shengsiyuan.struts2; 2 3 import java.util.Date; 4 5 public class LoginAction 6 { 7 private String username; 8 9 private String password; 10 11 private int age; 12 13 private Date date; 14 15 public Date getDate() 16 { 17 return date; 18 } 19 20 public void setDate(Date date) 21 { 22 this.date = date; 23 } 24 25 public int getAge() 26 { 27 return age; 28 } 29 30 public void setAge(int age) 31 { 32 this.age = age; 33 } 34 35 public String getUsername() 36 { 37 return username; 38 } 39 40 public void setUsername(String username) 41 { 42 this.username = username; 43 } 44 45 public String getPassword() 46 { 47 return password; 48 } 49 50 public void setPassword(String password) 51 { 52 this.password = password; 53 } 54 55 public String execute() 56 { 57 return "success"; 58 } 59 }
5.返回请求响应页
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'result.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 27 username: ${requestScope.username }<br> 28 password: ${requestScope.password }<br> 29 age: ${requestScope.age }<br> 30 date: ${requestScope.date } 31 </body> 32 </html>
代码结构
引用jar文件:struts-2.5.14.1-all.zip 文件中内容
额外注意:需要xwork-core-2.2.1.1.jar(不能上传文件)
http://localhost:8080/struts2/login.jsp 访问地址