struts2第一個程序 Helloworld


1. 新建項目: MyStruts2

2.導入相應的jar包:

3.修改web.xml文件:

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

小插曲:剛開始把filter寫成了servlet,導致一直報錯,后來才發現寫錯了,所以寫的時候要細心

4.在src下新建:struts.xml文件,內容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="helloworld" extends="struts-default">
        <action name="product-input">
            <result>/input.jsp</result>
        </action>
    </package>
</struts>

5.新建:index.jsp,主要代碼如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <a href="product-input.action">product-input</a>
</body>
</html>

6.新建:input.jsp,主要代碼如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    input.jsp
</body>
</html>

7.將項目部署到tomcat上並啟動

8.在瀏覽器地址欄:http://localhost:8080/MyStrust2/

9.點擊鏈接進入后結果如下:

10.項目結構如下:

================================================== 

11. 修改input.jsp,修改后如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="product-save">
        username:<input type="text" name="name"><br>
        password:<input type="text" name="password"><br>
        <input type="submit" value="login">
    </form>
</body>
</html>

12.修改struts.xml,添加action(product-save) ,修改后如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="helloworld" extends="struts-default">
        <action name="product-input">
            <result>/input.jsp</result>
        </action>
        
        <action name="product-save" class="com.xuzhiwen.action.Product" method="save">
            <result name="save">/save.jsp</result>
        </action>
    </package>
</struts>

13.新建類:com.xuzhiwen.action.Product ,內容如下:

package com.xuzhiwen.action;

public class Product {
    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;
    }
    
    @Override
    public String toString() {
        return "Product [name=" + name + ", password=" + password + "]";
    }
    
    public String save(){
        System.out.println("save()..."+this);
        return "save";
    }
    
}

14.添加save.jsp ,如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    username:${name}<br>
    password:${password}
</body>
</html>

15. 重啟啟動tomcat

16.點擊鏈接:

17.點擊login按鈕,結果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM