最近开始接触SSH框架了,刚开始对hibernate有了了解,现在开始对struts2进行实践,参照着书上边的步骤,今天搭建了我的第一个struts2环境,写出了第一个工程,感觉还是挺不错的,接下来我会更加深入的对struts2进行全面的了解。这是我的第一个工程,能够进行简单的登陆验证,
struts2.xml中的配置,struts2.xml新建在src文件夹下:
<?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="action" extends="struts-default" namespace="/"> <action name="login" class="action.LoginAction"> <result name="success">/success.jsp</result> <result name="input">/login.jsp</result> </action> </package> </struts>
package action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ private String username; //定义用户名属性 private String password; //定义用户密码属性 public String execute() throws Exception{ //对用户输入的用户名密码进行判断,如果正确返回success字符串 if(username.equals("admin") && password.equals("admin")){ return SUCCESS; //SUCCESS是action接口中的一个常量,值为success字符串 }else{ return INPUT;//如果错误,则返回input字符串 } } 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; } }
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name></display-name> <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> <init-param> <param-name>struts.il8n.encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> </web-app>
login.jsp,
<body> <s:form action="login" method="post" namespace="/"> <s:textfield name="username" label="用户名" cssStyle="width:160px;height:26px;"/> <s:password name="password" label="密码" cssStyle="width:160px;height:26px"/> <s:submit value="登录"/> </s:form> </body>
<body> 用户名:<s:property value="username"/> 密 码:<s:property value="password"/> </body>