您的位置:首页 > 编程语言 > Java开发

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

2015-02-17 10:09 274 查看
步骤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

显示信息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: