Struts2的简单例子配置第一个struts2例子

来源:互联网 发布:ae电子相册制作软件 编辑:程序博客网 时间:2024/05/27 20:11

步骤1:

导入struts2相关的jar包,MyEclipse里自带有,比较方便,

步骤2:

创建一个pojo类:

package wei.cheng;
public class LoginAction {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute ()throws Exception{
return "success";
}

}

步骤3:

创建一个名为Login.jsp

<form action="Login.action" method="post"><a>
  username: <input type="text" name="username"><br>
password:<input type="password" name="password"><br>
  <input type="submit" name="submit">
  </a></form>

步骤4:

创建一个result.jsp

    username:${requestScope.username}<br>
     password:${requestScope.password}

以上jsp比较简单,全部直接系统默认的代码都省略了,<html></html>.........

步骤5:

配置一个struts.xml 在src文件夹下,用来映射相关信息。

<?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="default" namespace="/" extends="struts-default">
<action name="Login" class="wei.cheng.LoginAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
步骤
6:

在web.xml里加载Struts2.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
     <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>


运行jsp显示,完成第一个struts2操作。在Login.jsp输入相关信息,result.jsp

显示信息。









0 0
原创粉丝点击