随笔 - 106  文章 - 0  评论 - 0  阅读 - 90921

ModelDriven

功能:

 submit 之后显示结果 

1.项目结构

2.web.xml

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

 

3.UserModel.java

复制代码
package com.action;

public class UserModel {
    private String name;
    private String age;
    private String address;
    private String telephone;
    
    //要注意 seter 与 getter 函数的名字一定要跟变量名一致 
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name=name;
    }
    
    public String getAge()
    {
        return age;
    }
    public void setAge(String age)
    {
        this.age=age;
    }
    
    public String getAddress()
    {
        return address;
    }
    public void setAddress(String address)
    {
        this.address=address;
    }
    
    public String getTelephone()
    {
        return telephone;
    }
    public void setTelephone(String telephone)
    {
        this.telephone=telephone;
    }

}
    
复制代码

 

4.LoginAction.java

复制代码
package com.action;

import com.action.UserModel;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LoginAction extends ActionSupport implements ModelDriven<UserModel>{
    private static final long serialVersionUID=1L;
    
    private UserModel myUser = new UserModel();
    
    public UserModel getModel()
    {
        return myUser;
    }
    
    public String execute() throws Exception{
        ActionContext context = ActionContext.getContext();
        context.put("user",myUser);
        return SUCCESS;
    }

}
复制代码

 

5.index.jsp

复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <center>
        <s:form action="user" method="post" namespace="/">  <!-- method="post" namespace="/" 可加可不加? -->
            <s:textfield label="Name:" name="name" />
            <s:textfield label="Age:" name="age" />
            <s:textfield label="Address:" name="address" />
            <s:textfield label="Telephone:" name="telephone" />
            <s:submit/>
        </s:form>
    </center>
</body>
</html>
复制代码

 

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="default" namespace="/" extends="struts-default">
        <action name="user" class="com.action.LoginAction">
            <result name="success">/success.jsp</result>
        </action>
    </package>
</struts>
复制代码

 

7.success.jsp

复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <center>
        <s:property value="#user.name" /><br/>                <!-- 不明白为什么 value="user.name" 而不能是 value="myUser.name" -->
        <s:property value="#user.age" /><br/>
        <s:property value="#user.address" /><br/>
        <s:property value="#user.telephone" /><br/>
    </center>
</body>
</html>
复制代码

 

posted on   程序员修仙之路  阅读(406)  评论(0编辑  收藏  举报
努力加载评论中...
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示