程序猿全敏的gravatar头像
程序猿全敏 2015-10-22 12:18:38

三大框架之struts2入门学习教程

以前学三大框架并没有学懂,现在重学,了解原理,并不是只会编码

struts1是基于servlet做的,struts2是基于过滤器来做的

struts2其他介绍我就不多说了,想必你们应该比我还熟悉,现在我就讲下搭建之类的问题

1.新建web项目

2.首先你要导入相关的包;因为新浪的bug问题我就发不了,你们也可以右击项目文件 点击myeclipse-Add Struts Capabilities选择Struts2.1

3.你的web.xml配置文件加上

 <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>

这个 /* 是拦截所有的请求

4.新建一个reg.jsp页面写一个form表单提交 请求到action

<form action="user" method="post">
        姓名:<input type="text" name="name"/><br>
        密码:<input type="password" name="password"/>
        <input type="submit" value="提交"/>
    </form>

这个action是等下点击提交按钮要跳转的action

5.新建一个UserAction类继承ActionSupport继承execute方法,其实也可以不继承,我只是难的写execute方法,所以继承了

@Override
    public String execute() throws Exception {
        return SUCCESS;
    }

6.新建一个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="user" extends="struts-default">
          <action name="user" class="com.qm.UserAction">
              <result name="index">/index.jsp</result>
          </action>
      </package>
  </struts>

package 的名字可以随便取

这里的action就是你reg.jsp页面提交到的Action class为处理的类
result为返回到哪!现在是返回到index.jsp页面

7.现在修改UserAction类里面的内容使得可以去到提交过来的内容。

    String name;
    String password;
    @Override
    public String execute() throws Exception {
        //System.out.println("测试成功");
        System.out.println("用户名:"+name);
        System.out.println("密码:"+password);
       //index是struts.xml返回的名字
        return "index";
    }
   
    public String getName() {
        return name;
    }
   
    public void setName(String name) {
        this.name = name;
    }
   
    public String getPassword() {
        return password;
    }
   
    public void setPassword(String password) {
        this.password = password;
    }

现在工作就完成了!打开浏览器输入reg.jsp地址访问,输入姓名课密码就可以取到了

工作原理是:点击提交经过web.xml过滤器文件到struts.xml找到Action的名字并得到相应的类。得到相应的值,跳转到index.jsp页面

同时你也可以建一个User实体,写上get set方法

private String name;
	private String password;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

然后你的UserAction,reg.jsp要做如下修改

<form action="user" method="post">
    	姓名:<input type="text" name="user.name"/><br>
    	密码:<input type="password" name="user.password"/>
    	<input type="submit" value="提交"/>
    </form>
        String name;
	String password;
	User user;

	@Override
	public String execute() throws Exception {
		//System.out.println("测试成功");
		//System.out.println("用户名:"+name);
		//System.out.println("密码:"+password);
		System.out.println(user.getName()+"="+user.getPassword());
		return "index";
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public User getUser() {
		return user;
	}
	
	public void setUser(User user) {
		this.user = user;
	}

希望大家可以指出我的不足的地方!毕竟我也才是新手

三大框架之struts2入门学习教程


打赏

文件名:struts2-1.rar,文件大小:3131.287K 下载
  • /
      • /struts2-1
        • /struts2-1/.classpath
        • /struts2-1/.mymetadata
        • /struts2-1/.project
          • /struts2-1/.settings
            • /struts2-1/.settings/.jsdtscope
            • /struts2-1/.settings/org.eclipse.core.resources.prefs
            • /struts2-1/.settings/org.eclipse.jdt.core.prefs
            • /struts2-1/.settings/org.eclipse.wst.common.component
            • /struts2-1/.settings/org.eclipse.wst.common.project.facet.core.xml
            • /struts2-1/.settings/org.eclipse.wst.jsdt.ui.superType.container
            • /struts2-1/.settings/org.eclipse.wst.jsdt.ui.superType.name
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友