准备工作:
Eclipse IDE For Java EE(4.7.2):官网下载地址
struts 2.5.14 : 官网下载地址(下载Full Distribution版)
测试例子源代码 :下载地址(留邮箱免费分享)
开始配置:
首先Eclipse的安装就不介绍了 ,Tomcat的配置前面博客有写过。
(1)创建动态Web项目
File -> New -> Dynamic Project 创建动态项目,创建名称为struts2 在Target runtime 中选择Apache Tomcat 7 ->Next
(2) ->Next(勾选下面的Generate web.xml......) ->Finish ,完成项目的创建。
(3)添加struts2 库到项目
在解压的Struts2.5.14..目录下的lib目录中找到以下需要的库,并粘贴到 struts2 项目的WebContent/WEB-INF/lib。
(4)配置过滤器 web.xml 文件
添加如下代码: <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(5)接下来就是编写Action
首先新建如下包 和 类
Login.javapackage org.com.action;
public class Login {
private String name;
private String credit;
private String period;
private int assessment;
private int term;
private String breif;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCredit() {
return credit;
}
public void setCredit(String credit) {
this.credit = credit;
}
public String getPeriod() {
return period;
}
public void setPeriod(String period) {
this.period = period;
}
public int getAssessment() {
return assessment;
}
public void setAssessment(int assessment) {
this.assessment = assessment;
}
public int getTerm() {
return term;
}
public void setTerm(int term) {
this.term = term;
}
public String getBreif() {
return breif;
}
public void setBreif(String breif) {
this.breif = breif;
}
}
LoginAction.javapackage org.com.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private Login login;
@Override
public String execute() throws Exception {
Map<String, Object> session = ActionContext.getContext().getSession();
session.put("login", login);
return "success";
}
public Login getLogin() {
return login;
}
public void setLogin(Login login) {
this.login = login;
}
}
package org.com.action;
public class Login {
private String name;
private String credit;
private String period;
private int assessment;
private int term;
private String breif;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCredit() {
return credit;
}
public void setCredit(String credit) {
this.credit = credit;
}
public String getPeriod() {
return period;
}
public void setPeriod(String period) {
this.period = period;
}
public int getAssessment() {
return assessment;
}
public void setAssessment(int assessment) {
this.assessment = assessment;
}
public int getTerm() {
return term;
}
public void setTerm(int term) {
this.term = term;
}
public String getBreif() {
return breif;
}
public void setBreif(String breif) {
this.breif = breif;
}
}
package org.com.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private Login login;
@Override
public String execute() throws Exception {
Map<String, Object> session = ActionContext.getContext().getSession();
session.put("login", login);
return "success";
}
public Login getLogin() {
return login;
}
public void setLogin(Login login) {
this.login = login;
}
}
(6)添加如下 jsp 页面
error.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>
error!
</body>
</html>
index.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">
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login</title>
</head>
<body>
<s:form action="login" method="post">
<s:textfield name="login.name" label="课名"></s:textfield>
<s:textfield name="login.credit" label="学分"></s:textfield>
<s:textfield name="login.period" label="学时"></s:textfield>
<s:radio list="#{1:'考试',2:'考查'}" label="考核" name="login.assessment" value="1"></s:radio>
<s:select list="#{1:'第一学期',2:'第二学期',3:'第三学期',4:'第四学期',5:'第五学期',6:'第六学期',7:'第七学期',8:'第八学期'}" label="学期" name="login.term"></s:select>
<s:textarea name="login.brief" label="简介"></s:textarea>
<s:submit value="添加"></s:submit>
<s:reset value="重置"></s:reset>
</s: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">
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Success</title>
</head>
<body>
添加课程信息成功 !
<br><br>
<s:set var="login" value="#session['login']"></s:set>
<s:property value="#login.name"/> : <s:property value="#login.credit"/> 学分,第<s:property value="#login.period"/>学期,考核方式为<s:property value="#login.assessment"/>.
</body>
</html>
(7) 最后配置struts.xml文件
右击src -> New -> Other -> XML 文件夹下的xml -> Next ->
填写struts.xml(必须是这个文件名) -> 一路默认
strust.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- Overwrite Convention -->
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.devMode" value="false"></constant> -->
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="org.com.action.LoginAction" >
<result name= "success">success.jsp</result>
<result name= "error">error.jsp</result>
</action>
</package>
</struts>
(8)最后在Tomcat上运行,如下是整个项目的目录:
<%@ 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>
error!
</body>
</html>
index.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">
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login</title>
</head>
<body>
<s:form action="login" method="post">
<s:textfield name="login.name" label="课名"></s:textfield>
<s:textfield name="login.credit" label="学分"></s:textfield>
<s:textfield name="login.period" label="学时"></s:textfield>
<s:radio list="#{1:'考试',2:'考查'}" label="考核" name="login.assessment" value="1"></s:radio>
<s:select list="#{1:'第一学期',2:'第二学期',3:'第三学期',4:'第四学期',5:'第五学期',6:'第六学期',7:'第七学期',8:'第八学期'}" label="学期" name="login.term"></s:select>
<s:textarea name="login.brief" label="简介"></s:textarea>
<s:submit value="添加"></s:submit>
<s:reset value="重置"></s:reset>
</s: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">
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Success</title>
</head>
<body>
添加课程信息成功 !
<br><br>
<s:set var="login" value="#session['login']"></s:set>
<s:property value="#login.name"/> : <s:property value="#login.credit"/> 学分,第<s:property value="#login.period"/>学期,考核方式为<s:property value="#login.assessment"/>.
</body>
</html>
右击src -> New -> Other -> XML 文件夹下的xml -> Next ->
填写struts.xml(必须是这个文件名) -> 一路默认
strust.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- Overwrite Convention -->
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.devMode" value="false"></constant> -->
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="org.com.action.LoginAction" >
<result name= "success">success.jsp</result>
<result name= "error">error.jsp</result>
</action>
</package>
</struts>
(8)最后在Tomcat上运行,如下是整个项目的目录: