Spring MVC初试

1. 所需的jar包:
  commons-logging.jar
  spring.jar
  spring-webmvc.jar

  if 你的view resolver指定为org.springframework.web.servlet.view.JstlView then
     添加 jstl.jar 包

2. 开始一个简单的login project

  configure web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>SpringWeb</display-name>
		
	<servlet>
		<servlet-name>Dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/Config.xml</param-value>
			<!-- <param-value>classpath:Config.xml</param-value> -->
		</init-param>
	</servlet>
	 
	<servlet-mapping>
		<servlet-name>Dispatcher</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
</web-app>


  configure Config.xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<!-- Definition of View Resolver -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass">
			<value>
				org.springframework.web.servlet.view.JstlView
			</value>
		</property>		
		<property name="prefix">
			<value>/WEB-INF/view/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
	
	<!-- Specified a URL Handler, Defined Request Mapping -->
	<bean id="urlMapping"
		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/login.do">loginAction</prop>
			</props>
		</property>
	</bean>
	<!-- Action Definition -->
	<bean id="loginAction"
		class="cn.devon.action.LoginAction">
		<property name="commandClass">
			<value>cn.devon.entity.User</value>
		</property>
		<property name="SUCCESS_VIEW" value="sucLogin"/>
		<property name="FAIL_VIEW" value="failLogin"/>
	</bean>
</beans>


Definition of index.jsp

<body>
	<form action="login.do" method="post">
		User Name: <input type="text" name="username"/> <br/>
		Password: <input type="text" name="password"/> <br/>

		<input type="submit" value="Submit"/>  <input type="reset" value="Reset"/>
	</form>
</body>

你可能感兴趣的:(spring,mvc,xml,jsp,Web)