1.新建web项目:
struts2101402
添加jar:
/struts2101402/WebRoot/WEB-INF/lib
commons-fileupload-1.3.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.15.1.jar
xwork-core-2.3.15.1.jar
src:
夹包: cn.jbit.struts2101401.domain
User:
private String userName;
private String password;
//提供有参无参构造函数
//get setter方法
cn.jbit.struts2101401.web.action
//列表action
ListAction:extends ActionSupport
private List<User> users;//get setter方法
/**
* 列表
* @return
*/
public String list(){
System.out.println("list");
setUsers(new ArrayList<User>());
User user1 = new User("guanyu","123");
User user2 = new User("zhangfei","123");
User user3 = new User("liubei","123");
getUsers().add(user3);
getUsers().add(user2);
getUsers().add(user1);
System.out.println("dsfdd");
return "list";
}
//登录action
LoginAction:extends ActionSupport
private User user;
@Override
public String execute() throws Exception {
if ("jbit".equals(user.getUserName()) && "123".equals(user.getPassword())) {
return SUCCESS;
}
return ERROR;
}
conf:
struts.xml:
<constant name="struts.ui.theme" value="simple"></constant>
<constant name="struts.devModel" value="true"></constant>
<package name="default" namespace="/" extends="struts-default">
<!-- 登录Action -->
<action name="loginAction" class="cn.jbit.struts2101401.web.action.LoginAction">
<!--
redirectAction:重定向到另一个Action
-->
<result name="success" type="redirectAction">
<param name="actionName">listAction</param> <!--重定向到名字为listAction的action-->
</result>
<result name="error">/WEB-INF/pages/error.jsp</result>
</action>
<!-- 查询列表Action -->
<action name="listAction" class="cn.jbit.struts2101401.web.action.ListAction" method="list">
<result name="list">/WEB-INF/pages/list.jsp</result>
</action>
</package>
index.jsp:
<s:form action="loginAction.action" method="post">
用户名:<s:textfield name="user.userName"/><br/>
密码:<s:password name="user.password"/><br/>
<s:submit value="登录"/>
</s:form>
list.jsp:
<table>
<tr>
<td>用户名</td>
<td>密码</td>
</tr>
<s:iterator value="users" status="s">
<tr>
<td>
<s:property value="userName"/>
</td>
<td>
<s:property value="password"/>
</td>
</tr>
</s:iterator>
</table>
success.jsp:登陆成功!
error.jsp: 登录失败!
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1564327
原文地址:http://suyanzhu.blog.51cto.com/8050189/1564327