标签:
功能:
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>
标签:
原文地址:http://www.cnblogs.com/c0liu/p/5496896.html