[关闭]
@Yano 2016-06-11T09:03:19.000000Z 字数 2485 阅读 2999

Struts2 Demo

Java-Web


下面两张图,用 viso 画了1个小时~ viso 很强大啊!

工作原理

Struts2的工作原理(图解)详解
Struts2基本原理

Struts2原理.png-163.3kB

工作流程

Struts2步骤.png-284.9kB

Hello World Demo

Struts 2 hello world (XML版本)

1.png-14.5kB

web.xml

配置 Struts 2

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <display-name></display-name>
  7. <welcome-file-list>
  8. <welcome-file>HelloWorld.jsp</welcome-file>
  9. </welcome-file-list>
  10. <!-- Struts2配置 -->
  11. <filter>
  12. <filter-name>struts2</filter-name>
  13. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  14. </filter>
  15. <filter-mapping>
  16. <filter-name>struts2</filter-name>
  17. <url-pattern>/*</url-pattern>
  18. </filter-mapping>
  19. </web-app>

struts.xml

配置每个Action。

  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. <struts>
  6. <package name="struts2" extends="struts-default">
  7. <global-results>
  8. <result name="login">/HelloWorld.jsp</result>
  9. </global-results>
  10. <action name="loginPerson" class="action.LoginAction">
  11. <result name="success">/welcome.jsp</result>
  12. </action>
  13. </package>
  14. </struts>

HelloWorld.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  3. <html>
  4. <head>
  5. <title>登录界面</title>
  6. </head>
  7. <body>
  8. <form action="loginPerson">
  9. <table>
  10. <tr>
  11. <td>账号</td>
  12. <td><input type="text" name="account"></td>
  13. </tr>
  14. <tr>
  15. <td>密码</td>
  16. <td><input type="password" name="password"></td>
  17. </tr>
  18. <tr>
  19. <td><input type="submit" value="登录"></td>
  20. </tr>
  21. </table>
  22. </form>
  23. </body>
  24. </html>

welcome.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  3. <html>
  4. <head>
  5. <title>欢迎界面</title>
  6. </head>
  7. <body>
  8. welcome!
  9. <%=request.getAttribute("account")%>
  10. </body>
  11. </html>

LoginAction.java

  1. package action;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class LoginAction extends ActionSupport {
  4. private String account;
  5. private String password;
  6. public String getAccount() {
  7. return account;
  8. }
  9. public void setAccount(String account) {
  10. this.account = account;
  11. }
  12. public String getPassword() {
  13. return password;
  14. }
  15. public void setPassword(String password) {
  16. this.password = password;
  17. }
  18. @Override
  19. public String execute() throws Exception {
  20. if ("yano".equals(account) && "123456".equals(password)) {
  21. return SUCCESS;
  22. }
  23. return LOGIN;
  24. }
  25. }

下载链接

http://pan.baidu.com/s/1pKNZix9

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注