标签:struts-登录2
1:创建项目
项目名:struts2101402
2:在文件夹中lib中添加jar包
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
3:在web.xml中添加控制器
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
4在src目录下创建包
包名:cn.jbit.struts2101401.domain
类名:User
public class User implements Serializable {
private String userName;
private String password;
//省了get and set
}
5:在src目录下创建包
包名:cn.jbit.struts2101401.web.action
类名:(控制类)UserAction
public class UserAction extends ActionSupport implements SessionAware{
//声明user对象
private User user;
private Map<String, Object> session;
private List<User> users;
@Override
public String execute() throws Exception {
if ("jbit".equals(user.getUserName()) && "123".equals(user.getPassword())) {
//把用户输入的用户名放在session中
session.put("username", user.getUserName());
return list();
}
return ERROR;
}
public String 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";
}
//省了get and set
}
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>
<constant name="struts.ui.theme" value="simple"></constant>
<package name="default" namespace="/ff" extends="struts-default">
<action name="userAction" class="cn.jbit.struts2101401.web.action.UserAction">
<result name="success">/WEB-INF/pages/success.jsp</result>
<result name="error">/WebRoot/WEB-INF/pages/error.jsp</result>
<result name="list">/WEB-INF/pages/list.jsp</result>
</action>
</package>
</struts>
7:创建视图页面
1>主视图
视图名index.jsp
<s:if test="%{#session.username != null}">
欢迎<s:property value="%{#session.username}"/>
</s:if>
<s:else>
<s:form action="/ff/userAction.action" method="post">
用户名:<s:textfield name="user.userName"/><br/>
密码:<s:password name="user.password"/><br/>
<s:submit value="登录"/>
</s:form>
</s:else>
2>列表视图
视图名:list.jsp
<table>
<tr>
<td>用户名</td>
<td>密码</td>
</tr>
//iterator迭代器
<s:iterator value="users" status="s">
<tr>
<td>
<s:property value="userName"/>
${s.index}
</td>
<td>
<s:property value="password"/>
</td>
</tr>
</s:iterator>
</table>
3>视图登录失败页面
视图名:error.jsp
<body>
登录失败!
</body>
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1564322
标签:struts-登录2
原文地址:http://suyanzhu.blog.51cto.com/8050189/1564322