reference : http://www.mkyong.com/struts2/struts-2-hello-world-example/
step 1
download : http://struts.apache.org/
step 2
copy from
/lib/common-logging-1.0.4.jar, commons-fileupload11.2.1.jar,freemarker-2.3.13.jar
struts2-core-2.1.6.jar, xwork-2.1.2.jar , ognl-2.6.11.jar
to
WEB-INF/lib
step 3
vi web.xml
<web-app> <display-name>Struts 2 Web Application</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> </web-app>Step 4
vi struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!-- XML声明 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts><!-- 根节点 -->
<constant name="struts.i18n.encoding" value="gb2312"></constant>
<package name="struts2" extends="struts-default">
<action name="register" class="com.project.action.RegisterAction">
<result name="success">/ShowUserInfo.jsp</result>
<result name="input">/Register.jsp</result>
</action>
</package>
</struts>
Adoption:--------------------------------------------Register.jsp
<%@page language="java" pageEncoding="gb2312"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="register.action" method="post">
<table>
<tr><td colspan="2"><s:actionerror/></td></tr>
<td><input type="text" name="username"></td></tr>
<td><input type="password" name="upassword"></td></tr>
<td> <input type="password" name="repassword"></td></tr>
<td><input type="text" name="name"></td></tr>
<td><input type="text" name="age"></td></tr>
<td><input type="text" name="birth"></td></tr>
<td><input type="text" name="email"></td></tr>
<tr><td><input type="submit" value="submit"></td>
<td><input type="reset" value="reset"></td></tr>
</table>
</form>
</body>
</html>
ShowUserInfo.jsp
<%@page language="java" pageEncoding="gb2312"%>
<html>
<head>
<title>Title</title>
</head>
<body>
${username}<br>
${upassword}<br>
${repassword}<br>
${name}<br>
${age}<br>
${birth}<br>
${email}<br>
</body>
</html>
Action
package com.project.action;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport{
private String username;
private String upassword;
private String repassword;
private String name;
private String age;
private String birth;
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUpassword() {
return upassword;
}
public void setUpassword(String upassword) {
this.upassword = upassword;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void validate() {
if(upassword == null || "".equals(upassword)){
this.addActionError("error");
}
if(repassword == null || "".equals(repassword)) {
this.addActionError("");
}
if(upassword != null && repassword != null && !repassword.equals(upassword)){
this.addActionError("");
}
}
public String execute() throws Exception {
return "success";
}
}
...................
沒有留言:
張貼留言