1、Struts2的一個登陸例項
阿新 • • 發佈:2018-12-10
1、新建專案,匯入包 2、com.action.StrutsAction
public class StrutsAction extends ActionSupport {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
@Override
public String execute() throws Exception {
if (!name.equals("hello")){
Map request= (Map) ActionContext.getContext().get("request");
request.put("name", getName());
return SUCCESS;
}else {
return ERROR;
}
}
}
3、配置struts.xml
<struts>
<package name="com.action" extends ="struts-default ">
<action name="StrutsAction" class="com.action.StrutsAction" method="execute">
<result name="success">/welcome.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
</struts>
4、配置web.xml
<welcome-file-list>
<welcome-file>hello.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>
5、新建hello.jsp登陸表單,點選登入指向StrutsAction
<body>
<form action="StrutsAction" method="post">
請輸入姓名:<input type="text" name="name">
<input type="submit" value="提交">
</form>
</body>
6、新建welcome.jsp StrutsAction攜帶值指向welcome.jsp
<body>
歡迎<%=request.getAttribute("name")%>
</body>