Struts1.x HelloWord

目录结构

 

Struts1.x HelloWord_第1张图片 

 

web.xml文件

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

 

 

sturts-config.xml

 

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <data-sources /> <form-beans> <!-- FormBean可以配置为静态FormBean与动态FormBean + 静态FormBean |- ActionForm |- ValidatorForm |- ValidatorActionForm + 动态FormBean |- DynaActionForm |- DynaValidatorForm |- DynaValidatorActionForm --> <form-bean name="user" type="com.wj.pojo.User"></form-bean> <form-bean name="productForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="productName" type="java.lang.String"></form-property> <form-property name="productPrice" type="java.lang.Double"></form-property> <form-property name="productDate" type="java.util.Date"></form-property> <form-property name="discription" type="java.lang.String"></form-property> </form-bean> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings> <!-- <action path="/地址" //拦截的地址 type="" //Action 具体地址 parameter="" //表示访问Action方法的标识,如:method=deleteUser 将访问deleteUser方法 input="" //指定验证错误返回的页面 validate="true" //是否启动输入的效验 scope="request" > <forward name="index" //在Action中,ActionMappding.findForward("index"),用户查找调转 path="/index.jsp" //跳转页面的具体地址 /> </action> --> <action path="/login" input="/index.jsp" validate="true" scope="request" parameter="method" type="com.wj.action.LoginAction" > <forward name="tag" path="/tag.jsp"></forward> <forward name="error" path="/error.jsp"></forward> </action> </action-mappings> <message-resources parameter="com.wj.struts.ApplicationResources" /> </struts-config>

 

 

 

Action

package com.wj.action; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import com.wj.pojo.User; public class LoginAction extends DispatchAction{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println(request.getParameter("userName")+" 开始验证登录."); System.out.println("密码为[ "+request.getParameter("password")+" ]"); System.out.println("LoginAction execute()"); return super.execute(mapping,form,request,response); } public ActionForward checkLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("开始检查用户是输入是否正确."); return mapping.findForward("error"); } public ActionForward toTag(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception{ List userList = new ArrayList(); userList.add("张三"); userList.add("李四"); userList.add("王五"); User user = new User(); user.setAge(33); user.setName("赵六"); userList.add(user); //设置 Bean到 request环境中 request.setAttribute("userList", userList); //跳转到 标签库页面 return mapping.findForward("tag"); } }

 

 

 

Pojo

 

package com.wj.pojo; import org.apache.struts.action.ActionForm; public class User extends ActionForm{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }

ApplicationResources.properties

time:properties读取的创建时间:2011-02-20 22:42:00

 

 

index.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 '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" mce_href="styles.css"> --> </head> <body> <form action="login.do?method=checkLogin" method="post"> <input name="userName"/> <input name="password" type="password"/> <input type="submit" value="登录"/> </form> <hr/> <a href="login.do?method=toTag" mce_href="login.do?method=toTag"><h2>Struts1.x 标签库</h2></a> </body> </html>

tag.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <%@taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %> <%@taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <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> <h1>Struts1.x tag</h1> <bean:message key="time" /> <h2>ArrayList 迭代</h2> <!-- ArrayList 迭代 --> <logic:iterate id="user" name="userList" indexId="index" scope="request"> <bean:write name="index"/> : <logic:notEqual value="3" name="index"> <bean:write name="user"/> <br/> </logic:notEqual> <logic:equal value="3" name="index"> name: <bean:write name="user" property="name"/> age: <bean:write name="user" property="age"/> <br/> </logic:equal> </logic:iterate> <hr/> <h2>HashMap 迭代</h2> <!-- 对map的遍历 --> <% java.util.HashMap countries = new java.util.HashMap(); countries.put("country1", "中国"); countries.put("country2", "美国"); countries.put("country3", "英国"); countries.put("country4", "法国"); countries.put("country5", "德国"); pageContext.setAttribute("countries", countries); %> <logic:iterate id="country" name="countries"> <bean:write name="country" property="key"/> : <bean:write name="country" property="value"/> <br/> </logic:iterate> <logic:iterate id="country" name="countries"> ${country.key} : ${country.value} <br/> </logic:iterate> <hr/> <h2>logic:equal</h2> <!-- logic:equal --> <!-- 判断是否相等 --> <%pageContext.setAttribute("equal",3); %> <logic:equal value="3" name="equal"> <%pageContext.setAttribute("equal",3); %> <br/> logic:equest value="3" name="equal" <br/> <u>如果结果通过,就能进入到logic:equal 块中</u> </logic:equal> <hr/> <!--logic:match --> <!-- location="end" 以x结尾 location="start" 以x开头 --> <h2>logic:match</h2> <%pageContext.setAttribute("match","Hello Struts taglib Struts"); %> <logic:match value="Struts" name="match" location="end"> true </logic:match> <hr/> <!-- logic:present --> <% pageContext.setAttribute("ExistingString", "teststring"); %> <logic:present name="ExistingString"> ExistingString的值为<bean:write name="ExistingString"/> </logic:present> <logic:notPresent name="ExistingString"> ExistingString的值为<bean:write name="ExistingString"/> </logic:notPresent> <hr/> <!-- logic:empty --> <% pageContext.setAttribute("empty", ""); %> <logic:empty name="empty"> empty 为空 </logic:empty> </body> </html>

error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <h1>Error Page</h1> </body> </html>

 

 

 

 

 

你可能感兴趣的:(Struts1.x HelloWord)