标签:servlet entity pass esrs org override throw string asp
目录
<dependencies>
<!-- spring-ioc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- Spring整合Web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Java EE-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>spring-web</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8888</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
dao
@Repository
public class userDaoImpl implements userDao {
@Override
public User selectByUsername(String username) {
User user=new User();
user.setUsername("toms");
user.setPassword("123");
user.setId(1001);
return user;
}
}
public interface userDao {
public User selectByUsername(String username);
}
service
@Service("uesrService")
public class userServiceImpl implements userService {
@Autowired
private userDao userDao;
@Override
public User loign(String username, String password) {
User user =userDao.selectByUsername(username);
if (user.getPassword().equals(password))
{
return user;
}
return null;
}
}
public interface userService {
public User loign(String username, String password);
}
entity
public class User implements Serializable {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
servlet
@WebServlet("/login")
public class userServlet extends HttpServlet {
private userService userService;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
userService= (com.itany.service.userService)SpringBeanHoder.getBean("uesrService");
User loign = userService.loign(username, password);
if(loign!=null)
{
req.setAttribute("username",username);
req.getRequestDispatcher("WEB-INF/view/success.jsp").forward(req,resp);
}
else
{
req.setAttribute("username",username);
req.getRequestDispatcher("WEB-INF/view/fail.jsp").forward(req,resp);
}
}
}
SpringBeanHoder
public class SpringBeanHoder implements ApplicationContextAware {
public static ApplicationContext ac;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContext.setApplicationContext");
ac=applicationContext;
System.out.println("ApplicationContext.endsetApplicationContext");
}
public static Object getBean(String bean)
{
return ac.getBean(bean);
}
public static <T> T getBean(Class<T> classz)
{
return ac.getBean(classz);
}
}
jsp
spring.xml
<context:component-scan base-package="com.itany.dao.impl"/>
<context:component-scan base-package="com.itany.service.impl"/>
<bean class="com.itany.until.SpringBeanHoder"/>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
标签:servlet entity pass esrs org override throw string asp
原文地址:https://www.cnblogs.com/lilihai/p/10142067.html