elentz

Untitled

Apr 26th, 2011
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. -------------- Action:
  2.  
  3. package com.struts2;
  4.  
  5. import java.util.Map;
  6.  
  7. import org.apache.struts2.interceptor.SessionAware;
  8.  
  9. import com.opensymphony.xwork2.Action;
  10. import com.opensymphony.xwork2.ActionSupport;
  11. import com.opensymphony.xwork2.ModelDriven;
  12.  
  13. public class TestAction extends ActionSupport implements SessionAware,
  14. ModelDriven<User> {
  15.  
  16. private static final long serialVersionUID = 1L;
  17.  
  18. private User user;
  19. private String userId;
  20.  
  21. private Map<String, Object> session;
  22.  
  23. public String list() {
  24. if (userId != null && userId.length() > 0) {
  25. System.out.println("userId was filled in with value: " + userId);
  26. }
  27. if (user != null) {
  28. if (user.getUserId() != null && user.getUserId().length() > 0) {
  29. System.out.println("User.userId was filled in with value: " + user.getUserId());
  30. }
  31. }
  32. return Action.SUCCESS;
  33. }
  34.  
  35. public User getUser() {
  36. return user;
  37. }
  38.  
  39. public void setUser(User user) {
  40. this.user = user;
  41. }
  42.  
  43. public String getUserId() {
  44. return userId;
  45. }
  46.  
  47. public void setUserId(String userId) {
  48. this.userId = userId;
  49. }
  50.  
  51. public User getModel() {
  52. user = (User) session.get("User");
  53. if (user == null) {
  54. user = new User();
  55. session.put("User", user);
  56. }
  57. return (user);
  58. }
  59.  
  60. public void setSession(Map<String, Object> session) {
  61. this.session = session;
  62. }
  63.  
  64. public Map<String, Object> getSession() {
  65. return session;
  66. }
  67. }
  68.  
  69. -------------- "Domain" class
  70.  
  71. package com.struts2;
  72.  
  73. public class User {
  74. private String userId;
  75. private String password;
  76. private String email;
  77.  
  78. public String getEmail() {
  79. return email;
  80. }
  81.  
  82. public void setEmail(String email) {
  83. this.email = email;
  84. }
  85.  
  86. public String getPassword() {
  87. return password;
  88. }
  89.  
  90. public void setPassword(String password) {
  91. this.password = password;
  92. }
  93.  
  94. public String getUserId() {
  95. return userId;
  96. }
  97.  
  98. public void setUserId(String username) {
  99. this.userId = username;
  100. }
  101.  
  102. }
  103.  
  104. -------------- struts.xml
  105.  
  106. <?xml version="1.0" encoding="UTF-8" ?>
  107. <!DOCTYPE struts PUBLIC
  108. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  109. "http://struts.apache.org/dtds/struts-2.0.dtd">
  110.  
  111. <struts>
  112.  
  113. <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  114. <constant name="struts.devMode" value="true" />
  115.  
  116. <package name="test" namespace="/" extends="struts-default">
  117.  
  118. <action name="test-*" class="com.struts2.TestAction" method="{1}">
  119. <result name="input">/jsp/test.jsp</result>
  120. <result name="success">/jsp/test.jsp</result>
  121. </action>
  122. </package>
  123. </struts>
  124.  
  125. -------------- jsp/test.jsp
  126.  
  127. <%@ page contentType="text/html; charset=UTF-8"%>
  128. <%@ taglib prefix="s" uri="/struts-tags"%>
  129. <html>
  130. <head>
  131. <title>Validation Struts page</title>
  132. <s:head />
  133. </head>
  134.  
  135. <body>
  136.  
  137. Intentionally empty. Please see stdout.
  138.  
  139. </body>
  140. </html>
  141.  
  142. -------------- web.xml
  143.  
  144. <?xml version="1.0" encoding="UTF-8"?>
  145. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  146. <display-name>Struts2ParmTest</display-name>
  147. <filter>
  148. <filter-name>struts2</filter-name>
  149. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  150. </filter>
  151. <filter-mapping>
  152. <filter-name>struts2</filter-name>
  153. <url-pattern>/*</url-pattern>
  154. </filter-mapping>
  155. <welcome-file-list>
  156. <welcome-file>index.html</welcome-file>
  157. <welcome-file>index.htm</welcome-file>
  158. <welcome-file>index.jsp</welcome-file>
  159. <welcome-file>default.html</welcome-file>
  160. <welcome-file>default.htm</welcome-file>
  161. <welcome-file>default.jsp</welcome-file>
  162. </welcome-file-list>
  163. </web-app>
  164.  
  165. -------------- log4j.xml
  166.  
  167. <?xml version="1.0" encoding="UTF-8" ?>
  168. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  169.  
  170. <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  171.  
  172. <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
  173. <param name="Target" value="System.out"/>
  174. <param name="Threshold" value="DEBUG"/>
  175. <layout class="org.apache.log4j.PatternLayout">
  176. <!-- The default pattern: Date Priority [Category] Message\n -->
  177. <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] %m%n"/>
  178. </layout>
  179. </appender>
  180.  
  181. <root>
  182. <priority value ="debug" />
  183. <appender-ref ref="CONSOLE" />
  184. </root>
  185.  
  186. </log4j:configuration>
Advertisement
Add Comment
Please, Sign In to add comment