Tutorial_Create Struts 2 Application in Eclipse(5)
发布时间:2021-06-08
发布时间:2021-06-08
Specify folder name resources and press Finish. Create a file ApplicationResources.properties under resources folder.
Copy following content in ApplicationResources.properties. ername= Username label.password= Password label.login= Login
The JSPWe will create two JSP files to render the output to user. Login.jsp will be the starting point of our application which will contain a simple login form with username and password. On successful authentication, user will be redirected to Welcome.jsp which will display a simple welcom
e message. Create two JSP files Login.jsp and Welcome.jsp in WebContent folder of your project. Copy following content into it.
Login.jsp<%@ page contentType="text/html; charset=UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><html><head><title>Struts 2 - Login Application| </title></head><body><h2>Struts 2 - Login Application</h2><s:actionerror/><s:form action="login.action" method="post"><s:textfield name="username" key="ername" size="20"/><s:password name="password" key="label.password" size="20"/><s:submit method="execute" key="label.login" align="center"/>
</s:form></body></html>
Welcome.jsp
<%@ page contentType="text/html; charset=UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><html><head><title>Welcome</title></head> <body> <h2>Howdy, <s:property value="username" />...!</h2></body></html>
Note that we have used struts2 <s:> tag to render the textboxes and labels. Struts2 comes with a powerfulbuilt-in tag library to render UI elements more efficiently.
The struts.xml file
Struts2 reads the configuration and class definition from an xml file called struts.xml. This file is loadedfrom the classpath of the project. We will define struts.xml file in the resources folder. Create file struts.xmlin resources folder.
Copy following content into struts.xml.
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <constant name="struts.custom.i18n.resources" value="ApplicationResources" /> <package name="default" extends="struts-default" namespace="/"> <action name="login" class="net.viralpatel.struts2.LoginAction"> <result name="success">Welcome.jsp</result> <result name="error">Login.jsp</result> </action> </package></struts>
Note that in above configuration file, we have defined Login action of our application. Two result paths are
下一篇:胰头癌并发症