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="true" />
- <constant name="struts.devMode" value="true" />
- <include file="helloworld.xml"/>
- </struts>
<script>render_code();</script>
HelloWorld.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>
- <include file="struts-default.xml"/>
- <package name="helloworld" namespace="/" extends="struts-default">
- <interceptors>
- <interceptor name ="authentication" class ="com.jamesby.struts2.LogonInterceptor"/>
- <interceptor-stack name="helloWorldStack">
- <interceptor-ref name="authentication"/>
- <interceptor-ref name="paramsPrepareParamsStack"/>
- </interceptor-stack>
- </interceptors>
- <default-interceptor-ref name="helloWorldStack"/>
- <global-results>
- <result name="login">/WEB-INF/jsp/Logon.jsp</result>
- </global-results>
- <action name="Logon_*" method="{1}" class="com.jamesby.struts2.Logon">
- <interceptor-ref name="paramsPrepareParamsStack"/>
- <result type="redirect-action">Main</result>
- <result name="input">/WEB-INF/jsp/Logon.jsp</result>
- </action>
- <action name="crud" method="{1}" class="com.jamesby.struts2.UserAction">
- <result name="list">/WEB-INF/jsp/userList.jsp</result> <result name="success" type="redirect-action">crud!list</result>
- <result name="input">/WEB-INF/jsp/userForm.jsp</result>
- </action>
- <action name="*" class="com.jamesby.struts2.ExampleSupport">
- <result>/WEB-INF/jsp/{1}.jsp</result>
- </action>
- </package>
- </struts>
<script>render_code();</script>
LogonInterceptor.java
代码
- package com.jamesby.struts2;
- import java.util.Map;
- import com.jamesby.struts2.persist.User;
- import com.opensymphony.xwork2.Action;
- import com.opensymphony.xwork2.ActionInvocation;
- import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
- public class LogonInterceptor extends AbstractInterceptor{
- @Override
- public String intercept(ActionInvocation ai) throws Exception {
- Map session = ai.getInvocationContext().getSession();
- User user = (User) session.get("user");
- if (null != user) {
- return ai.invoke();
- } else {
- return Action.LOGIN;
- }
- }
- }
<script>render_code();</script>
Logon.java
代码
- package com.jamesby.struts2;
- import java.util.Map;
- import org.apache.struts2.interceptor.SessionAware;
- import org.apache.struts2.interceptor.validation.SkipValidation;
- import com.jamesby.struts2.persist.DataBaseMock;
- import com.jamesby.struts2.persist.User;
- import com.opensymphony.xwork2.ActionSupport;
- public class Logon extends ActionSupport implements SessionAware {
- private Map session;
- public void setSession(Map session) {
- this.session = session;
- }
- @SkipValidation
- public String doInput() throws Exception {
- return INPUT;
- }
- public String doLogon() throws Exception {
- User user = DataBaseMock.selectUserByUsernameAndPassword(getUsername(),
- getPassword());
- if (null == user)
- return INPUT;
- session.put("user", user);
- return SUCCESS;
- }
- private String username;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- private String password;
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
<script>render_code();</script>
UserAction.java
代码
- package com.jamesby.struts2;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import org.apache.struts2.interceptor.validation.SkipValidation;
- import com.jamesby.struts2.persist.DataBaseMock;
- import com.jamesby.struts2.persist.User;
- import com.opensymphony.xwork2.ActionSupport;
- public class UserAction extends ActionSupport {
- private User user=new User();
- private List<User> userList;
- public List<HashMap> getSexArray() {
- List<HashMap> sexArray = new ArrayList<HashMap>();
- HashMap male = new HashMap();
- male.put("key",new Integer(1));
- male.put("value","男");
- HashMap female = new HashMap();
- female.put("key",new Integer(2));
- female.put("value","女");
- sexArray.add(male);
- sexArray.add(female);
- return sexArray;
- }
- public List<HashMap> getFromArray() {
- List<HashMap> fromArray = new ArrayList<HashMap>();
- HashMap bj = new HashMap();
- bj.put("id",new Integer(1));
- bj.put("name","北京");
- HashMap sh = new HashMap();
- sh.put("id",new Integer(2));
- sh.put("name","上海");
- HashMap tj = new HashMap();
- tj.put("id",new Integer(3));
- tj.put("name","天津");
- fromArray.add(bj);
- fromArray.add(sh);
- fromArray.add(tj);
- return fromArray;
- }
- @SkipValidation
- public String doList() throws Exception {
- userList = DataBaseMock.selectAllUser();
- return "list";
- }
- @SkipValidation
- public String doInput() {
- return INPUT;
- }
- public String doSave() {
- if (null == user.getUserid()) {
- user.setUserid(new Integer(DataBaseMock.getNewUserId()));
- DataBaseMock.saveUser(user);
- }else
- {
- DataBaseMock.updateUser(user);
- }
- return SUCCESS;
- }
- @SkipValidation
- public String doRead()
- {
- user = DataBaseMock.getUser(user.getUserid());
- return INPUT;
- }
- @SkipValidation
- public String doDelete() {
- DataBaseMock.deleteUser(user);
- return SUCCESS;
- }
- public List<User> getUserList() {
- return userList;
- }
- public void setUserList(List<User> userList) {
- this.userList = userList;
- }
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- }
<script>render_code();</script>
ExampleSupport.java
代码
- package com.jamesby.struts2;
- import com.opensymphony.xwork2.ActionSupport;
- public class ExampleSupport extends ActionSupport {
- }
<script>render_code();</script>
User.java
代码
- package com.jamesby.struts2.persist;
- public class User {
- private Integer userid;
- private String username;
- private String password;
- private int sex;
- private int age;
- private int from;
- public int getFrom() {
- return from;
- }
- public void setFrom(int from) {
- this.from = from;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public int getSex() {
- return sex;
- }
- public void setSex(int sex) {
- this.sex = sex;
- }
- public Integer getUserid() {
- return userid;
- }
- public void setUserid(Integer userid) {
- this.userid = userid;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
<script>render_code();</script>
DataBaseMock.java
代码
- package com.jamesby.struts2.persist;
- import java.util.ArrayList;
- import java.util.List;
- public class DataBaseMock {
- private static List<User> _cache = new ArrayList<User>();
- private static int userid = 0;
- static {
- User user = new User();
- user.setUserid(new Integer(0));
- user.setUsername("admin");
- user.setPassword("12345688");
- user.setFrom(1);
- user.setSex(1);
- user.setAge(18);
- _cache.add(user);
- }
- public static User selectUserByUsernameAndPassword(String username,String password)
- {
- for (User u : _cache) {
- if (u.getUsername().equals(username)&&u.getPassword().equals(password)) {
- return u;
- }
- }
- return null;
- }
- public static List<User> selectAllUser()
- {
- return _cache;
- }
- public static int getNewUserId() {
- return ++userid;
- }
- public static void saveUser(User user) {
- _cache.add(user);
- }
- public static void updateUser(User user) {
- deleteUser(user);
- saveUser(user);
- }
- public static void deleteUser(User user) {
- for (User u : _cache) {
- if (u.getUserid().intValue() == user.getUserid().intValue()) {
- _cache.remove(u);
- break;
- }
- }
- }
- public static User getUser(Integer userid) {
- return _cache.get(userid);
- }
- }
<script>render_code();</script>
Logon-validation.xml
代码
- <!DOCTYPE validators PUBLIC
- "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
- "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
- <validators>
- <field name="username">
- <field-validator type="requiredstring">
- <message key="requiredstring"/>
- </field-validator>
- </field>
- <field name="password">
- <field-validator type="requiredstring">
- <message key="requiredstring"/>
- </field-validator>
- </field>
- </validators>
<script>render_code();</script>
UserAction-validation.xml
代码
- <!DOCTYPE validators PUBLIC
- "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
- "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
- <validators>
- <field name="user.username">
- <field-validator type="requiredstring">
- <message key="errors.required.username"/>
- </field-validator>
- </field>
- <field name="user.password">
- <field-validator type="requiredstring">
- <message key="errors.required.password"/>
- </field-validator>
- </field>
- <field name="user.sex">
- <field-validator type="required">
- <message key="errors.required.sex"/>
- </field-validator>
- </field>
- <field name="user.age">
- <field-validator type="required" short-circuit="true">
- <message key="errors.required.age"/>
- </field-validator>
- <field-validator type="int">
- <param name="min">18</param>
- <param name="max">65</param>
- <message key="errors.required.age.limit"/>
- &