Struts2学习笔记之开发环境搭建
Struts2学习笔记之开发环境搭建
对于struts2现在用的是相当的多啊,,不学看来就得落伍了,记下一系列的学习笔记,供以后复习使用。
首先还是从所有程序员都认识的HelloWorld开始。
1、加入必要的开发包,这里所列的包是从struts2自带的demo中拷贝出来的。有如下的一些。
2、配置web.xml:
[html]
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <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>
- </web-app>
Action可以是普通的POJO、还可以是继承ActionSupport的类、或是实现Action的类,对于继承或实现的方式,里面会有一些常量,方便作为返回值使用
[java]
- Action.ERROR ;
- Action.INPUT ;
- Action.LOGIN ;
- Action.NONE ;
- Action.SUCCESS ;
[java]
- package com.akwolf.helloworld.action;
- import com.opensymphony.xwork2.ActionSupport;
- public class LoginAction extends ActionSupport {
- private static final long serialVersionUID = 1L;
- private String username;
- private String password;
- @Override
- public String execute() throws Exception {
- if ("akwolf".equals(username) && "123456".equals(password)) {
- return "success";
- }
- return "error";
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
4、进行struts.xml的配置
[html]
- <?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>
- <!-- 包机制防止重名,action也必须放在package下 ,可以继承自一个包-->
- <package name="helloworld" extends="struts-default" namespace="/">
- <!-- 具体的用来请求处理逻辑的action,并指定使用哪一个action进行处理 -->
- <action name="login" class="com.akwolf.helloworld.action.LoginAction">
- <!-- action处理后的返回结果,根据结果进行相应的视图跳转 -->
- <result name="success">/welcom.html</result>
- <result name="error">/error.html</result>
- </action>
- </package>
- </struts>
index.jsp
[html]
- <body>
- <form action="login.action" method="post">
- <table align="center">
- <caption><h3>用户登录</h3></caption>
- <tr>
- <td>
- <span style="white-space:pre"> </span>用户名:<input type="text" name="username" />
- </td>
- </tr>
- <tr>
- <td>
- <span style="white-space:pre"> </span>密码:<input type="text" name="password"/>
- </td>
- </tr>
- <tr align="center">
- <td>
- <span style="white-space:pre"> </span><input type="submit" value="登录"/>
- <span style="white-space:pre"> </span><input type="reset" value="重置"/>
- </td>
- </tr>
- </table>
- </form>
- </body>
[html]
- <body>
- <h3>欢迎方位<a href="index.jsp">返回</a></h3>
- </body>
error.html
[html]
- <body>
- <h3>信息错误!!<a href="index.jsp">返回</a></h3>
- </body>
评论暂时关闭