调了好久Struts2 这是一个简单的登录例子.
1、安装javaee版 eclipse 下载地址:http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/junor
2、下载struts2压缩包。解压apps/struts2-blank.war文件。
3、建一个Dynamic Web Project,将上述2中解压得到文件的apps\struts2-blank\WEB-INF\lib内jar包复制到WebContent/WEB-INF/lib目录下
4、改写web.xml,需要加一个过滤器
- <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>
- <?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_2_5.xsd" id="WebApp_ID" version="2.5">
- <display-name>struts2test</display-name>
- <welcome-file-list>
- <welcome-file>Login.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>
- package action;
- import com.opensymphony.xwork2.ActionSupport;
- public class LoginAction extends ActionSupport{
- private String username;
- private String password;
- public String execute() {
- if(username.equals("asd")&&password.equals("asd"))
- 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;
- }
- }
6、在存放java代码的目录下(src)创建一个struts.xml,里面的内容:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
- "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <constant name="struts.devMode" value="true"></constant>
- <constant name="struts.i18n.encoding" value="utf-8"></constant>
- <package name="default" extends="struts-default">
- <action name="loginAction" class="action.LoginAction">
- <result name="success"> index.jsp</result>
- <result name="error"> error.jsp</result>
- </action>
- </package>
- </struts>
7、编写一个Login.jsp:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!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>
- <form action="loginAction">
- 用户名:<input type="text" name="username"/><br/>
- 密码:<input type="text" name="password"/><br/>
- <input type="submit" value="登录" name="登录"/>
- </form>
- </body>
- </html>
8、然后再写一个index.jsp 和 error.jsp即可
注意:在web.xml中的welcome-file 修改为Login.jsp 这样网站默认的首页就是这个登录页面了
文章部分来源于:http://blog.csdn.net/cwb_where/article/details/7934509