标签:
Struts2启动配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version="3.1"> 6 <filter> 7 <filter-name>struts2</filter-name> 8 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 9 </filter> 10 <filter-mapping> 11 <filter-name>struts2</filter-name> 12 <url-pattern>/*</url-pattern> 13 </filter-mapping> 14 </web-app>
Struts2主配置文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 9 <constant name="struts.devMode" value="true" /> 10 <!--默认就是UTF-8 所以不配置也行--> 11 <!--<constant name="struts.i18n.encoding" value="UTF-8"/>--> 12 13 <package name="default" namespace="/" extends="struts-default"> 14 <action name="index"> 15 <result>/WEB-INF/main.jsp</result> 16 </action> 17 </package> 18 19 <!--struts2qs模块--> 20 <include file="org/zln/struts2qs/cfg/struts2_struts2qs.xml"/> 21 22 </struts>
Struts2模块文件配置
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <!--国际化资源--> 9 <constant name="struts.custom.i18n.resources" value="org/zln/struts2qs/resource/messageResource"/> 10 11 <!--struts2qs模块--> 12 <package name="struts2qs" namespace="/struts2qs" extends="struts-default"> 13 <!--登录界面--> 14 <action name="loginUI" class="org.zln.struts2qs.action.LoginAction" method="loginUI"> 15 <result name="success">/WEB-INF/struts2qs/loginForm.jsp</result> 16 </action> 17 <!--登录动作--> 18 <action name="loginDo" class="org.zln.struts2qs.action.LoginAction" method="loginDo"> 19 <result name="success">/WEB-INF/struts2qs/welcome.jsp</result> 20 <result name="input">/WEB-INF/struts2qs/loginForm.jsp</result> 21 </action> 22 <!--获取书籍--> 23 <action name="getBooks" class="org.zln.struts2qs.action.BookAction" method="getBook"> 24 <result name="success">/WEB-INF/struts2qs/showBooks.jsp</result> 25 <result name="login">/WEB-INF/struts2qs/loginForm.jsp</result> 26 </action> 27 </package> 28 29 </struts>
Action
1 package org.zln.struts2qs.action; 2 3 import com.opensymphony.xwork2.ActionContext; 4 import com.opensymphony.xwork2.ActionSupport; 5 import org.apache.commons.lang3.StringUtils; 6 7 /** 8 * Created by sherry on 15-7-8. 9 */ 10 public class LoginAction extends ActionSupport { 11 private String username; 12 private String password; 13 14 //登录界面 15 public String loginUI(){ 16 return SUCCESS; 17 } 18 //登录动作 19 public String loginDo(){ 20 System.out.println("获取参数:" + username + ":" + password); 21 if ("zln".equals(username) 22 &&"123".equals(password)){ 23 ActionContext.getContext().getSession().put("user",username); 24 return SUCCESS; 25 }else { 26 return INPUT; 27 } 28 } 29 30 //输入校验 31 32 //所有方法的输入校验 33 @Override 34 public void validate() { 35 super.validate(); 36 } 37 38 //指定方法的输入校验 39 public void validateLoginDo(){ 40 if (StringUtils.isEmpty(username)){ 41 //对错误信息进行了国际化 也可以直接用文字 42 addFieldError("username",getText("username.required")); 43 } 44 } 45 46 public String getUsername() { 47 return username; 48 } 49 50 public void setUsername(String username) { 51 this.username = username; 52 } 53 54 public String getPassword() { 55 return password; 56 } 57 58 public void setPassword(String password) { 59 this.password = password; 60 } 61 } 62 63 64 /* 65 Action访问Servlet API 66 间接访问 67 static ActionContext getContext() 68 直接访问 69 * */
1 package org.zln.struts2qs.action; 2 3 import com.opensymphony.xwork2.ActionContext; 4 import com.opensymphony.xwork2.ActionSupport; 5 import org.zln.struts2qs.service.BookService; 6 7 /** 8 * Created by sherry on 15-7-8. 9 */ 10 public class BookAction extends ActionSupport { 11 private String[] books; 12 13 public String getBook(){ 14 String user = (String) ActionContext.getContext().getSession().get("user"); 15 if ("zln".equals(user)){ 16 //实际开发中会从容器中获取依赖对象 17 BookService bookService = new BookService(); 18 books = bookService.getLeeBooks(); 19 return SUCCESS; 20 }else { 21 return LOGIN; 22 } 23 } 24 25 public String[] getBooks() { 26 return books; 27 } 28 29 public void setBooks(String[] books) { 30 this.books = books; 31 } 32 }
jsp页面
1 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> 2 <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> 3 <%@ taglib prefix="s" uri="/struts-tags" %> 4 <% 5 String home = request.getContextPath(); 6 %> 7 <html> 8 <head> 9 <%--s:text标签输出国际化信息--%> 10 <title><s:text name="loginPage"/></title> 11 </head> 12 <body> 13 <form action="<%=home%>/struts2qs/loginDo.action" method="post"> 14 <table> 15 <caption>用户登录</caption> 16 <tr> 17 <td>用户名:</td> 18 <td><input type="text" name="username" /></td> 19 </tr> 20 <tr> 21 <td>密 码:</td> 22 <td><input type="password" name="password" /> </td> 23 </tr> 24 <tr> 25 <td colspan="2"> 26 <input type="submit" value="提交"/> 27 <input type="reset" value="重置" /> 28 </td> 29 </tr> 30 </table> 31 </form> 32 <hr/> 33 <%--使用Struts2标签 默认提供表单输出提示--%> 34 <s:form namespace="/struts2qs" action="loginDo" method="POST"> 35 <%--使用标签的 key 输出国际化信息--%> 36 <s:textfield name="username" key="username"/> 37 <s:password name="password" label="密 码"/> 38 <s:submit value="提交"></s:submit> 39 </s:form> 40 </body> 41 </html>
1 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> 2 <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> 3 <%@ taglib prefix="s" uri="/struts-tags" %> 4 <% 5 String home = request.getContextPath(); 6 %> 7 <html> 8 <head> 9 <%--使用表达式输出国际化信息--%> 10 <title><s:property value="%{getText(‘succPage‘)}"/> </title> 11 </head> 12 <body> 13 欢迎,${sessionScope.user},您已经登录!<br/> 14 <a href="<%=home%>/struts2qs/getBooks.action">获取书籍列表</a> 15 </body> 16 </html>
1 <%@ page import="com.opensymphony.xwork2.util.ValueStack" %> 2 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> 4 <%@ taglib prefix="s" uri="/struts-tags" %> 5 <html> 6 <head> 7 <title>书籍</title> 8 </head> 9 <body> 10 <table border="1" width="360"> 11 <% 12 /*Struts2的所有属性参数被封装在此*/ 13 ValueStack valueStack = (ValueStack) request.getAttribute("struts.valueStack"); 14 String[] books = (String[]) valueStack.findValue("books"); 15 for (String book:books){ 16 %> 17 <tr> 18 <td>书名:</td> 19 <td><%=book%></td> 20 </tr> 21 <% 22 } 23 %> 24 </table> 25 <hr/> 26 <table border="1" width="360"> 27 <caption>书籍</caption> 28 <s:iterator value="books" status="index"> 29 <s:if test="#index.odd == true"> 30 <tr style="background-color: aqua"> 31 </s:if> 32 <s:else> 33 <tr> 34 </s:else> 35 <td>书名:</td> 36 <td><s:property/></td> 37 </tr> 38 </s:iterator> 39 </table> 40 <hr/> 41 <a href="${pageContext.request.contextPath}/index.action">返回首页</a> 42 </body> 43 </html>
Action访问Servlet API的方法
标签:
原文地址:http://www.cnblogs.com/sherrykid/p/4631050.html