struts2 session用法

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

//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>
      
        <package name="default" extends="struts-default">
        <action name="login" class="Login">
        <result>success.jsp</result>
        </action>
        </package>
        </struts>

//Login.java

import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;


public class Login extends ActionSupport implements SessionAware {
private Map session;

private String username;
private String password;

public void setSession(Map session) {
   this.session=session;

}

public String execute(){
   User user=User.createUser(username, password);
  
   session.put("msg", user);
  
   return SUCCESS;
}

public String getUsername() {
   return username;
}

public void setUsername(String str) throws UnsupportedEncodingException {
   username = new String(str.getBytes("ISO-8859-1"),"GBK");
   System.out.println(username);
  
}

public String getPassword() {
   return password;
}

public void setPassword(String password) {
   this.password = password;
}

}

//User.java


public class User {
public User(){}
public User(String name,String password){
   this.name=name;
   this.password=password;
}
public static User createUser(String name,String password){
   return new User(name,password);
   }
private String name;
private String password;
public String getName(){
return this.name;
}

}

//index.jsp

<%@ page contentType="text/html;charset=gb2312"%>

<%@taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  
   
    <title>Login</title>

</head>

<body>
    <s:form action="login">
    <s:textfield label="用户名" name="username"></s:textfield>
    <s:password label="密 码" name="password"/>
    <s:submit value="提交"></s:submit>
    </s:form><br>
</body>
</html>

//success.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title>Hello World!</title>
</head>
<body>
    <h2>
        <s:property value="message" escape="false"/>
        <br>Message from session: <s:property value="#session.msg"/>
    </h2>
</body>
</html>

posted @ 2008-11-13 20:55  Earl_86  阅读(5627)  评论(0编辑  收藏  举报