一个struts2程序
1.index.jsp
2.struts.xml
3.Loginaction.java
package action; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.Map; import java.io.PrintStream; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; //Action本质上是一个POJO:Plain Old Java Object(JavaBean) public class Loginaction extends ActionSupport { private String user, pass; //private private String mail_address,telephone,birth_date; public String execute(){ System.out.println("User:" + user + ",Pass:" + pass); if(user == null || user.length() == 0) return "fair"; ActionContext context = ActionContext.getContext();//action 的上下文对象 //Map session = context.getSession();//actiontext本身就是一个session Map<String,Object> session =context.getSession(); session.put("user", user); session.put("pass", pass); session.put("mail_address", mail_address); session.put("telephone", telephone); //FileOutputStream out =null; try { File file = new File("D://file.txt"); //FileOutputStream fs = new FileOutputStream(file); PrintStream ps =null; ps =new PrintStream(file); ps.println(user);// 往文件里写入字符串 ps.append(pass);// 在已有的基础上添加字符串 } catch (FileNotFoundException e) { e.printStackTrace(); } return SUCCESS; } public String getMail_address() { return mail_address; } public void setMail_address(String mail_address) { this.mail_address = mail_address; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } }
4.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" 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>teststruts2</display-name> <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> <!-- 配置Struts2过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> </web-app>
5.pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>teststruts2maven2</groupId> 4 <artifactId>teststruts2maven2</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <dependencies> 7 <dependency> 8 <groupId>javax.servlet</groupId> 9 <artifactId>servlet-api</artifactId> 10 <version>2.5</version> 11 </dependency> 12 <dependency> 13 <groupId>org.apache.struts</groupId> 14 <artifactId>struts2-core</artifactId> 15 <version>2.5.16</version> 16 </dependency> 17 </dependencies> 18 </project>
当你在凝视深渊时,深渊也在凝视你。