Struts.xml 文件
代码
- <!---->xml version="1.0" encoding="UTF-8" ?>
- <!---->
- "-//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" ?>
- <!---->
- "-//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.jspresult>
- global-results>
- <action name="Logon_*" method="{1}" class="com.jamesby.struts2.Logon">
- <interceptor-ref name="paramsPrepareParamsStack"/>
- <result type="redirect-action">Mainresult>
- <result name="input">/WEB-INF/jsp/Logon.jspresult>
- action>
- <action name="crud" method="{1}" class="com.jamesby.struts2.UserAction">
- <result name="list">/WEB-INF/jsp/userList.jspresult> <result name="success" type="redirect-action">crud!listresult>
- <result name="input">/WEB-INF/jsp/userForm.jspresult>
- action>
- <action name="*" class="com.jamesby.struts2.ExampleSupport">
- <result>/WEB-INF/jsp/{1}.jspresult>
- 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></user> userList;
- public List<hashmap></hashmap> getSexArray() {
- List<hashmap></hashmap> sexArray = new ArrayList<hashmap></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></hashmap> getFromArray() {
- List<hashmap></hashmap> fromArray = new ArrayList<hashmap></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></user> getUserList() {
- return userList;
- }
- public void setUserList(List<user></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></user> _cache = new ArrayList<user></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></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
代码
- <!---->
- "-//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
代码
- <!---->
- "-//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
|
相关推荐
这个"struts2-tutorial-lesson2.zip"压缩包包含了一个基础的Struts2入门实例,是SourceForge.net上的经典教学资源。SourceForge作为一个开源项目托管平台,为开发者提供了许多高质量的学习材料。 这个入门实例...
Struts2入门实例通常会涉及如何创建一个基本的web应用,实现对数据的增删查改操作,并与数据库进行交互。 在"struts2入门实例"中,我们首先会接触到Struts2的核心概念。这包括Action类,它扮演控制器的角色,负责...
对于初学者,除了这个入门实例,还可以参考Struts1的官方文档、教程和在线课程,深入了解Struts1的工作原理、配置细节以及最佳实践。 总之,Struts1入门实例是一个很好的起点,它让你能快速搭建一个简单的Web应用...
这个"struts2入门例子"旨在帮助初学者理解Struts2的基本架构和核心功能,通过实际操作来学习如何配置Struts2框架以及实现页面跳转。 在Struts2中,`struts.xml`是核心配置文件,它是整个应用的入口点,负责定义动作...
总之,这个"struts2入门教程+实例源代码"是初学者学习Struts2的理想资源,它提供了从零开始构建Struts2应用的全面指导。通过学习,你将能够创建自己的Struts2应用,并为进一步的Web开发打下坚实基础。在实践中不断...
以上就是 Spring 和 Struts 的基础概念及入门实例的创建流程。这两个框架各有优势,Spring 更注重模块化和灵活性,而 Struts 则更侧重于 MVC 结构的实现。在实际项目中,两者常被一起使用,形成所谓的“Spring + ...
本入门实例将带你逐步了解Struts2的基础知识,帮助你快速掌握其核心概念。 首先,我们需要理解Struts2框架的基本架构。它基于拦截器(Interceptor)机制,通过配置Action类和结果视图来处理用户请求。Action类是...
本资源包提供了Struts入门的学习材料,包括实例代码和PDF讲解文件,非常适合初学者理解和掌握Struts框架。 1. **Struts 框架基础** - **MVC模式**:Struts基于Model-View-Controller(MVC)设计模式,通过分离业务...
首先,Struts2是一个基于MVC(Model-View-Controller)架构的框架,它提供了一种组织应用代码的方式,使得业务逻辑、数据模型和用户界面能够有效地分离。在本入门案例中,你可能会看到以下几个核心组件: 1. **...
通过这个入门实例,你可以逐步了解并掌握Struts2.0的各个关键元素,理解其工作流程,并能够创建和运行自己的Struts2应用程序。实际操作过程中,结合源码分析,将有助于加深对框架原理的理解,为后续的Java Web开发...
这个入门教程包含了源码和实例,旨在帮助初学者快速理解并掌握Struts2的核心概念和工作原理。 首先,让我们深入了解一下Struts2的基础知识。Struts2是Apache软件基金会的开源项目,它是Struts1的升级版,融合了...
这个经典入门实例将引导我们深入了解Struts2的核心概念和实践操作,特别是通过包含过滤器的设置来增强应用的功能和安全性。 首先,我们要明白Struts2的工作原理。它是基于Model-View-Controller(MVC)设计模式的,...
在本入门实例中,我们将探讨如何使用Struts2创建一个简单的Web应用程序。 首先,我们需要确保具备正确的开发环境。在本教程中,使用的是MyEclipse6作为开发工具,Tomcat6作为Web服务器,Struts2的版本为2.0.11.1,...