照葫芦画瓢系列之Java --- eclipse下使用maven创建Struts 2项目
一、创建Maven项目
http://www.cnblogs.com/zhanqun/p/8425571.html
二、添加struts2核心依赖包以及其他依赖项
打开pom.xm配置界面
点击Add按钮添加struts2-core.jar
选中并点击Ok添加进入项目即可;添加完成后如下
三、新建Action
1、新建package名为cn.smallbyte.mavendemo.action
2、在包内创建以UserAction命名的class,继承自com.opensymphony.xwork2.ActionSupport
UserAction.java内容如下
package cn.smallbyte.mavendemo.action; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L; public String execute() { return SUCCESS; } public String login() { try { HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("name->" + username + ",password->" + password); if ("admin".equals(username) && "123456".equals(password)) { return SUCCESS; } else { return "login"; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return SUCCESS; } }
3、配置struts2的核心配置文件(src/main/resources下struts.xml文件),内容如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="mavendemoAction" namespace="/demo" extends="struts-default"> <action name="login" class="cn.smallbyte.mavendemo.action.UserAction" method="login"> <result name="success">/success.jsp</result> <result name="login">/login.jsp</result> </action> </package> </struts>
4、配置/src/main/webapp/WEB-INF/web.xml,内容如下:
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>mavendemo</display-name> <!-- struts2 --> <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>*.action</url-pattern> </filter-mapping> </web-app>
5、创建web页面
struts.xml文件中配置了success.jsp/login.jsp两个页面
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>Login Page</title> </head> <body> <form action="${pageContext.request.contextPath}/demo/login.action" method="post"> username:<input type="text" name="username" value="" /><br /> password:<input type="text" name="password" value="" /> <br /> <input type="submit" value="提交" /> </form> </body> </html>
success.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>Login Success</title> </head> <body>Login Success </body> </html>
6、编译
鼠标右键点击项目,然后依次选择
Run As -> Maven Install直到出现如下Build Success消息表示编译成功
7、发布
鼠标右键项目 -> Run As -> Run On Server -> Manually define a new server ->Tomcat v7.0 Server
点击finish完成。
8、验证
在浏览器输入:http://localhost:8080/mavendemo/demo/login.action
输入admin/123456后提交后登录成功并跳转到success.jsp页面