Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------- Action:
- package com.struts2;
- import java.util.Map;
- import org.apache.struts2.interceptor.SessionAware;
- import com.opensymphony.xwork2.Action;
- import com.opensymphony.xwork2.ActionSupport;
- import com.opensymphony.xwork2.ModelDriven;
- public class TestAction extends ActionSupport implements SessionAware,
- ModelDriven<User> {
- private static final long serialVersionUID = 1L;
- private User user;
- private String userId;
- private Map<String, Object> session;
- public String list() {
- if (userId != null && userId.length() > 0) {
- System.out.println("userId was filled in with value: " + userId);
- }
- if (user != null) {
- if (user.getUserId() != null && user.getUserId().length() > 0) {
- System.out.println("User.userId was filled in with value: " + user.getUserId());
- }
- }
- return Action.SUCCESS;
- }
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- public String getUserId() {
- return userId;
- }
- public void setUserId(String userId) {
- this.userId = userId;
- }
- public User getModel() {
- user = (User) session.get("User");
- if (user == null) {
- user = new User();
- session.put("User", user);
- }
- return (user);
- }
- public void setSession(Map<String, Object> session) {
- this.session = session;
- }
- public Map<String, Object> getSession() {
- return session;
- }
- }
- -------------- "Domain" class
- package com.struts2;
- public class User {
- private String userId;
- private String password;
- private String email;
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getUserId() {
- return userId;
- }
- public void setUserId(String username) {
- this.userId = username;
- }
- }
- -------------- struts.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constant name="struts.enable.DynamicMethodInvocation" value="false" />
- <constant name="struts.devMode" value="true" />
- <package name="test" namespace="/" extends="struts-default">
- <action name="test-*" class="com.struts2.TestAction" method="{1}">
- <result name="input">/jsp/test.jsp</result>
- <result name="success">/jsp/test.jsp</result>
- </action>
- </package>
- </struts>
- -------------- jsp/test.jsp
- <%@ page contentType="text/html; charset=UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <html>
- <head>
- <title>Validation Struts page</title>
- <s:head />
- </head>
- <body>
- Intentionally empty. Please see stdout.
- </body>
- </html>
- -------------- web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <display-name>Struts2ParmTest</display-name>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
- -------------- log4j.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
- <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
- <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
- <param name="Target" value="System.out"/>
- <param name="Threshold" value="DEBUG"/>
- <layout class="org.apache.log4j.PatternLayout">
- <!-- The default pattern: Date Priority [Category] Message\n -->
- <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] %m%n"/>
- </layout>
- </appender>
- <root>
- <priority value ="debug" />
- <appender-ref ref="CONSOLE" />
- </root>
- </log4j:configuration>
Advertisement
Add Comment
Please, Sign In to add comment