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

首个struts2 DEMO制作

2015-08-25 13:36 567 查看
转载自:http://blog.csdn.net/ocean20/article/details/3216910

struts官方网站:

 http://jakarta.apache.org/struts

 http://jakarta.apache.org/struts/userGuide/introduction.html

开发环境:

IDE:MyEclipse6.0

Servlet Contenter: Tomcat5.5

JDK:Java2开发平台标准版5.0

步骤:

一、首先建立一个Web Project 工程

二、新建一个login.jsp页面

  <body>

  <form action="login.action" method="post">

        username:<input type=text name=username>

  <br>

  <br>password:<input type=password name=password>

  <br><input type=submit value=登陆>

  </form>

  </body>

三、新建一个result.jsp页面

  <body>

  

username:${requestScope.username }<br>

password:${requestScope.password }<br>

  </body>

四、在WebRoot目录下的web.xml 添加红色部分的代码

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>

</web-app>

五、在工程的src目录下新建一个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="struts2" extends="struts-default">

<action name="login" class="com.test.action.LoginAction">

<result name="success">/result.jsp</result>

</action>

</package>

</struts>

六、在src下新建一个包com.test.action,并新建一个java类,代码如下

package com.test.action;

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

}

}

七、手动配置Struts2所需5个jar包 ,将struts2包中的lib中如下四个文件

拷贝到工程的如下图位置

八、Tomcat5/conf/server.xml  中配置工程 新增一行如下代码

<Context path="/struts2" docBase="D:/MyDemo-workplace/MyStruts2Demo1/WebRoot" reloadable="true"/>

九、运行tomcat

十、浏览器中输入http://localhost:8080/struts2/login.jsp,如下效果

十一、点击登陆后执行login.action,跳转到result.jsp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: